Lesson 2-Character Set and Keywords
Lesson 2-Character Set and Keywords
Uppercase characters A to Z
Lowercase characters a to z
Digits 0 to 9
Mathematical symbol +, - , = and some more special characters
are used in C for building the program elements, like identifiers,
constants, variable, operators, expressions, etc.
The alphabets, numbers and special symbols when properly
combined form constants, variables and keywords.
#include<stdio.h>
main()
{
int x;
x=5;
printf(“%d is the value”, x);
}
We already know the use of the first lines. The first line includes the library
function into our program. The second line declares the main function of our
program. With the curly braces in the third line the body of the main () function
starts. The fourth line int x; is a declaration of variable x. In C, a variable has to
be declared before it can be used.
The variable is always declared by mentioning its type. Here x
is an integer type of a variable. It means its value can only be
a whole number. The next line assigns value 5 to the variable
x.
(try to change this line to some fraction like 5.3, you will find
that the program accepts but prints only the integer part so
the output is 5 is the value not 5.3.
int i,j,k;
short int s;
For example, int is a standard data type, but while declaring age of
students you want to use the word age as a type of variable (which is
actually an integer type) rather than using the keyword int.
/*
This program demonstrates the simple use of typedef statement.
It calculates the area of a rectangle. It is uses typedef to redefine the
word side as int specifier.
*/
#include<stdio.h>
main()
{
typedef int side;
side x=2,y=3;
printf(“the area of the rectangle is %d units\n”, x*y);
}
Assigning Value to a Variable
int x,y;
x=2;
y=x+3;
int x,y,i;
x=2;
i=x+y;
int x, y, i;
x=y=i=2;
x value is 2 x=2;
y value is 2 y=2;
i value is also 2 i=2;
The following is also valid
int a, b, c;
c=5;
a=b= ((c=c+4) +4));
int x=2;
}
In the example above , we have used a variable type of float. To
print the variable value we have used a format %f which indicates
that variable is to be printed as float type.
#include<stdio.h>
int main()
{
// variable declaration
int x,y,answer;
// input
printf("\n Enter 1st number: “);
OUTPUT
scanf("%d",&x);
//process
answer=x-y;
//output
printf("\n The answer is= %d\n", +answer);
}