In C, variables are used to store the value which can be changed and can be reused. The variables are the name of memory block used in the program. It is simply the location with which we are working.
A variable is defined as a meaningful name given to a data storage location in computer memory. When using a variable, we actually refer to address of the memory where the data is stored. C language supports two basic kinds of variable:
C is relatively low-level programming language, before working in the program memory blocks are needed to declare which is done by the compiler.
Numeric Variable: Numeric variable can be used to store either integer value or floating point values. While an integer value is a whole number without a fraction part or decimal point, a floating point value can have a decimal point.
Numeric variable may also be associated with modifiers, such as short, long, signed, and unsigned. The difference between signed and unsigned numeric variables is that signed variables can be either negative or positive but unsigned variables can only be positive. Therefore, by using an unsigned variable we can increase the maximum positive range.
When we do not specify the signed/unsigned modifier, C language automatically takes it as a variable. To declare an unsigned variable, the unsigned modifier must be explicitly added during the declaration of the variable.
Character Variable: Character variable are just single characters enclosed within single quotes. These characters could be any character from the ASCII character set—letters (‘a’, ‘A’), numerals (‘2’), or special character (‘&’). In C, a number that is given in single quotes is not the same as a number without them. This is because 2 are treated as an integer value but ‘2’ is a considered character not an integer.
Declaring Variables: Each variable to be used in the program must be declared. To declare a variable, specify the data type of the variable followed by its name. The data type indicates the kind of values that the variable will store.
Variable names should always be meaningful and must reflect the purpose of their usage in the program. The memory location of the variable is of importance to the compiler only and not to the programmer. Programmers must only be concerned with accessing data through their symbolic names. In C, variable declaration always ends with a semicolon, for example:
unsigned short int acc_no;
In C variable can be declare at any place in the program but two things must be kept in mind. First, variable should be declared before using them. Second, variables should be declared closest to their first point of use to make the source code easier to maintain.
C allows multiple variables of the same type to be declared in one statement. So the following statement is legal in C.
Float temp_in_celsius, tem-in_farenheit;
In C variables are declared at three basic places as follows:
- When a variable is declared inside a function it is known as a local variable.
- When a variable is declared in the definition of function parameters it is known as a formal parameter.
- When the variable is declared outside all functions, it is known as a global variable.
Note: A variable cannot be of type void.
Initializing Variable: While declaring variables, we can also initialize them with some value. For example,
char grade= ‘A’;
double bal_amt= 1000000000;
The initialization applies only to the variable defined immediately before it. Therefore, the statement
int count, flag=1;
It initializes the variable flag and not count. If you want both the variables to be declared in a single statement then write,
int count=0, flag=1;
When variables are declared but not initialized they usually contain garbage values.