Variable
It is a name given to storage area and its value change during program execution.
It is used to store data , and its value change at any time.
Rules for defining variable
1. Variable name must start with alphabet and underscore.
2. Space are not allowed.
3. Keywords are not allowed.
4. Special symbol are not allowed.
5. Digits are allowed but first character must be alphabet or underscore.
6. Variable length should be 1 to 8 character long.
Syntax for variable declaration
Syntax:- type variable_name;
EX:- Example for declaring a variable.
int a;
float b;
here a, b are a variable and int , float are datatype .
There are five types of variable available in C they are given below.
a) local variable
b) global variable
c) static variable
d) automatic variable
e) external variable
Q> Why only underscore is allowed in variable declaration ?
Ans> There are following reason to used underscore.
1. Underscore is best and understandable if we used in any variable definition.
2. It provide understandable readable format.
3. It is the best way to separate two names
Local variable
These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program.
Global Variable
A variable that is declared outside the function or block or body is called global variable. If any function changes the value of the global variable so it will be changed for all.
It must be declared at the start of the block.
Static Variable
A variable that is declared with static keyword is called static variable.
It is declared at once and never changed before leaving the program.
Automatic Variable
All variables declared inside the block, are automatic variables by default. But we can also explicitly declare automatic variable using auto keyword.
External Variable
We can share a variable in multiple C source files by using external variable. To declare a external variable, we need to use extern keyword.
some time in tourbo compiler extern keyword does not allowed so you can declare variable without extern keyword.