Variables are the name given to memory locations where different values of different length can be store . As its name suggest , its value can vary during the execution of the program . C supports different type of variables depending upon the data-type .
eg. int (integer) variable normallytake 2 bytes and store Integer value ie. 1 , 5 , 4 etc.
char(character) variable always take 1 byte and can store only one character in it . ie. 'A' , 'Z' , 'X' , 'y' etc.
I want to tell you some rules for selecting the variable name.
The first character must be alphabet or underscore( _ ) . eg. HsinghK , _HSK .
A variable name must be between 1 to 31 alphabets or underscores .
No commas or blank spaces are allowed in a variable name .
No special symbol other from underscore is allowed .
Now after knowing the basic rules to construct the variable name i tell you the basic syntax to declare a variable .
(data_type) variable_name ;
eg. int var1 ;
In this example , int (integer) is data-type of variable & var1 is the name of variable .
According to data-type this variable takes 2 byte space in memory and it can store integer value between the range .
( -32768 to + 32767 ) .
In this variable , if we assign some value out of the range , it change the value according to its limit . I show you how the compiler change the value according to its range .
int var ;
printf(" Enter some value ") ;
scanf(" %d " , &var ) ;
printf(" Entered value is = %d " , var ) ;
Now we take some examples : -
1) Enter some value
2600 /* 2600 is number between -32768 and +32767 so OK */
Entered value is = 2600
2) Enter some value
32767 /* 32767 is number between -32768 and +32767 so OK */
Entered value is = 32767
3) Enter some value
32768 /* 32768 is NOT in range -32768 and +32767 so compiler change this value */
Entered value is = - 32768
4) Enter some value
32769 /* 32769 is NOT in range -32768 and +32767 so compiler change this value */
Entered value is = - 32767 /* The compiler change the value as */
5) Enter some value
32774 /* 32774 is NOT in range -32768 and +32767 so compiler change this value */
Entered value is = - 32762 /* The compiler change the value as */
If we try to see how the compiler calculate , the resulting Table is formed : -