Variables in C Programming
Variables may be the most fundamental aspect of any computer language. A variable is a space in the computer's memory set aside for a certain kind of data and given a name.
Variables are used so that the same space in memory can hold different values at different times.
Constant and Variables
Printf( ) function can be used to print constant number, and characters. For example:
void main ( )
{
printf("This is the number five: %d", 5);
}
Output
This is the number five: 5
printed the number 5 plugging it into the format specifier, %d,
of course this is not very useful, since we could more easily have written:
void main ( )
{
printf("This is the number five: 5");
}
To achieve the same result the power of computer language comes from the ability to use variable, which can hold many different values in program statement. Let's rewrite the program above to use a variable instead of a constant.
void main ( )
{
int num;
num=5;
printf("This is the number five: %d", num);
}
This program gives the same output as before, but is has achieved it in quite a different way. It creates a variabl num, assigns it the value 5, and then prints out the value contained in the variable. This program contains several new elements. in the first statement,
int num;
a variable is declared, it is given a name and a type (type int).
in the second statement,
num=5;
The variable is assigned a value. The assignment operator (=) is used for this purpose.
In the third statement of the program, the variable name,num is used as one of the arguments of the printf( ).
Variable Definitions
The statement
int num;
is an example of a variable definition. The definition consists of the type, int, followed by the name of the variable, num. In a C program all variables must be defined. if you have more than one variable of a same type, you can define them all with one type name, separating the variable names with commas:
int pencils, books, notebooks;
When you define a variable , the compiler sets aside an appropriate amount of memory to store that variable. In the present case we've specified an integer variable, so compiler will set aside two bytes of memory to hold numbers from -32768 to 32767.
Variable Types
in our previous program we used integer data type, there are other variable types as well.
Most variable types are numeric but there is one that isn't numeric. the character type.
A character variable value may be a letter or other character surrounded by single quotes. A character variable is one-byte memory space in which the character constants such as 'a' or 'A' can be stored. The type name for a character is char. Thus, to declare a character variable, called ch, you would use the statement:
char ch;
You already know about integers. For situations when the normal integer is too small, the long integer (type long or long int) can be used . It occupies four bytes of memory and can hold integers from -2,147,483,648 to 2,147,483,647.
There are also two kinds of floating point variables. Floating point numbers are used to represent values that are measured like the height of a person(5 feet 6 inches). Floating point variable type float occupies four bytes and can hold numbers from about 1038 to 10-38 .
A double precision floating point variable, type double, occupies eight bytes and can hold numbers from 10308 to 10-308.
Another variable type, short is a two byte integer just like int and therefore is seldom used.
The Character type and Integer types also have unsigned versions (type unsigned char, unsigned int and unsigned long) which change the range of numbers the type can hold. For instance the unsigned int type holds numbers from 0 to 65,535 rather than from -32768 to 32767.
Initializing Variables
It is possible to combine a variable definition with an assignment operator so that a variable is given a value at the same time its's defined for example;
int event=5;

