Variables
As you know, "A variable is a named location in memory that is used to hold a value that can be modified by the program". All variables must be declared before they can be used.
Variable declaration:-
General form of declaring a variable is:-
"type variable_list;"
here "type" is the data type you want to use and variable_list is the name of identifiers(variable/varibles).
e.x:- int i; here i is name of variable of integer type.
int a,bcd,c_i; here a,bcd,c_i are names of variables of integer type.
Types of variables:-
1.Local variables:- These are the variables declared inside a function. These are also called as "automatic variable". It can be used only from inside the program i.e it can be used only by the statements that are inside the block in which the variables are declared.
e.x. -
void func(void)
{
int a; <- here a is a integer type variable which is locally declared.
a=20;
}
2.Global variables:- These are the variables declared outside the functions.It can be used anywhere in whole program.
e.x-
#include
int a; <- here a is a integer variable declared globally.
void sum
{
int b,c; ------|
c=b+a; |
} |
void sub | here a is used in both the functions.
{ |
int b,c; |
c=a-b; ------|
}
3.Formal parameters:- If a function is to use arguments, it must declare variables that will accept the values of the arguments.These variables are calledformal parameters. They behaves like other local variables.
e.x.-
int ab(char *a, char c)
{
while(*a)
if(*a==c) return 1;
else a++;
return 0;
}
Comments
Post a Comment