Variables
in C
A variable is an identifier used to represent some
specified type of information . A variable represents a single data item , that
is a numerical quantity or a character constant etc.
The data item must be assigned to the variable at
some point in the program. The data items can be accessed later in the program
by simply referring to variable name.
The information represented by the variable can
change during the execution of the program by assigning different data items at
various places with in the program.
But the data type associated with the variable
cannot change.
Variable Declaration :
Before a variable is used in c program it must be
declared.
The syntax for variable declaration is a shown
below:
Datatype
(space) Varaiablename;
Example:
int a ;
here, int is the data typeof the variable
there
should be a space between the datatype and variablename
a is
the variable name
each
statement in c should be terminated by ;(semicolon)
That means the ‘a’ is a variable and it can store
only integer values.
We can also declare multiple variables of similar
datatypes as follows:
Datatype variablename_1,variablename_2,..,variablename_n;
Example:
float a,b,c,d,e;
here all the variables a,b,c,d,e belongs to float
type.
Char name[20];
Here name is a character string which can store up
to 19 characters followed by a null character.
Initial values can be assigned to the variables at
the type declaration itself.
The syntax for it is as follows:
Datatype
variablename = value;
Example:
Char star = ‘b’;
int num =4;
that means star now holds the value b and num holds
the value 4.
“ = “ (equal
to) is called as assignment operator because it assigns the value from right to
left.
Statements:
A statement causes the computer to carry some
action.
There are two types of statements in c. They are
· simple
· compound
A simple statement consist of expression followed by
semi colen.
Example :
main()
{
int a,b;
a=5;
b=a+6;
}
The above statements says that…
Line 1: a and b are integer variables ,
Line 2: the store the value of a is 5,
Line 3: add the value at variable a with 6 , the
value of a is 5, so 5+6 is 11 .
Example of compound statement,
{
int pi=3.14;
circumference
= 2*pi*radius;
area=pi*radius*radius;
}
The compound statement provides the capability of
embedding statements with in other statements.
Thank you …