1 Introduction to C
1 Introduction to C
1 Introduction to C
Language
History of C
Why name “C” was given to Language?
Many of C’s principles and ideas were derived from the earlier
language B. (Ken Thompson was the developer of B Language.)
#include <stdio.h>
int main (void)
{
printf ("Programming is fun.\n");
printf ("And programming in C is even more fun
return 0;
}
Displaying multiple lines of text
#include <stdio.h>
int main (void)
{
printf ("Testing...\n..1\n...2\n....3\n"
return 0;
}
Output:
It is not necessary
to make a separate
call to printf for each Testing...
line of output ! ..1
...2
....3
Variables
• Programs can use symbolic names for
storing computation data and results
• Variable: a symbolic name for a memory
location
– programmer doesn’t has to worry about
specifying (or even knowing) the
location/address
• In C, variables have to be declared before
they are used
Using and Displaying Variables
#include <stdio.h>
int main (void)
{
int sum;
sum = 50 + 25;
printf ("The sum of 50 and 25 is %i\n", sum)
return 0;
}
Variable sum declared of type int
Variable sum assigned expression 50+25
Value of variable sum is printed in place of %i
The printf routine call has now 2 arguments: first argument a string
containing also a format specifier (%i), that holds place for an integer
value to be inserted here
printf function
Displaying multiple values
#include <stdio.h>
int main (void)
{
int value1, value2, sum;
value1 = 50;
value2 = 25;
sum = value1 + value2;
printf ("The sum of %i and %i is %i\n",value1, value2, sum
return 0;
}
…..
…… Program statements
……
Closing brace
} End of program
The main ( ) is a special function used by the C system to tell the
computer where the program starts. Every program must have
exactly one main function. The empty parentheses immediately
following main indicates the function main has no arguments.
main ( ) If we compile this program, it shows
three errors.
{ Function printf should have a
printf ( “ ”) prototype
Statement missing
} Compound statement missing
# include <stdio.h>
int main ( )
{
printf ( “Hello, How are you? ”);
return 0;
}
# include <stdio.h> /* header file for printf ( ) */
# include <conio.h> /* header file for getch ( ) */
main ()
{
/* this is a comment */
Tokens in C
A C program consists of various tokens and a token is
either a keyword, an identifier, a constant, a string
literal, or a symbol. For example, the following C
statement consists of five tokens:
/* my first program in C */
Identifiers
A C identifier is a name used to identify a variable,
function, or any other user-defined item. An
identifier starts with a letter A to Z or a to z or an
underscore _ followed by zero or more letters,
underscores, and digits (0 to 9).
C does not allow punctuation characters such as
@, $, and % within identifiers. C is a case
sensitive programming language. Thus, Manpower
and manpower are two different identifiers in C.
Here are some examples of acceptable identifiers:
Keywords
The following list shows the reserved words in C.
These reserved words may not be used as
constant or variable or any other identifier names.
Whitespace in C
A line containing only whitespace, possibly with a comment, is
known as a blank line, and a C compiler totally ignores it.
Whitespace is the term used in C to describe blanks, tabs, newline
characters and comments. Whitespace separates one part of a
statement from another and enables the compiler to identify where
one element in a statement, such as int, ends and the next element
begins. Therefore, in the following statement:
int age;