Variables are means for location in memory used by a program to store data. The size of that block depends upon the range over which the variable is allowed to vary.
For example, on personal computer the size of an integer variable is two bytes, and that of a long integer is four bytes.
A variable region is temporarily remember a number or string value, such as covered by the program. To identify the variables, you have a name unique to every single variable. This is called a variable name. Before using a variable, use variables to what is called a variable declaration that you have to reveal the names and data types that can be stored in the variable variable.
The format for declaring a variable in C.
|[Storage-class] type data variable name [= initial value];|
- Storage class and the initial value can be omitted.
- The same data type and storage class variable can be declared, separated by commas.
|[Storage-class] type data variable name [= initial value] variable [= initial value] variable [= initial value];|
- In C the size of a variable type such as an integer need not be the same on all types of machines. When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type character with the name i by writing:
- On seeing the “char” part of this statement the compiler sets aside one bytes of memory to hold the value of the character. It also sets up a symbol table. In that table it adds the symbol i and the relative address in memory where those one byte was set aside. Thus, later if we write: i = ‘x’; we expect that,at run time when this statement is executed, the value ‘x’ will be placed in that memory location reserved for the storage of the value of i.
Following are the rules for naming the variables:
- All variables must be declared before they can appear in executable statement.
- A declaration consists of a data type followed by one or more variable names separated by commas.Example: int a,b,c;
- Variables can be distributed among declarations in any fashion. The above declaration can be written asint a;int b,c;
- Integer type variables can be declared to be short integer for smaller integer quantities or long integer for larger integer quantities.Example:short int a,b,c;long int a,b,c;
- An integer variable can also be declared to be un signed by writing unsigned int.
Example: unsigned int;
C – VARIABLES EXAMPLE PROGRAM :
int x= 10;
printf(“Value of x = %d\n ”, x);
printf(“Value of x = %d\n”,x);
|Value of x = 10
Value of x = 20
C – VARIABLE EXAMPLE PROGRAM :
printf(“\nvalue of n = %d”,n);
value of n=75