2. Variable
Introduction
○ In programming, a variable is a container (storage
area) to hold data.
○ To indicate the storage area, each variable should be
given a unique name or identifier.
○ Variable names are just the symbolic representation of
a memory location.
Ex: int MathScore = 95;
○ Here, MathScore is a variable of integer type.
The variable is assigned value: 95.
2
5. “
5
• A variable name can have letters (both uppercase and lowercase letters), digits and
underscore only.
Ex: score, Math, player1, _customer, a, B, counter_value , node2node, etc
• The first letter of a variable should be either a letter or an underscore. However, it is
discouraged to start variable name with an underscore. It is because variable name that starts
with an underscore can conflict with system name and may cause error.
Ex: sub_level, _bank, WhiteColor etc
• There is no rule on how long a variable can be. However, only the first 31 characters of a
variable are checked by the compiler. So, the first 31 letters of two variables in a program
should be different.
7. “
7
○ A variable declaration provides assurance to the compiler that there exists a variable with the
given type and name so that the compiler can proceed for further compilation without
requiring the complete detail about the variable.
○ A variable definition has its meaning at the time of compilation only, the compiler needs
actual variable definition at the time of linking the program.
○ Though you can declare a variable multiple times in your C program, it can be defined only
once in a file, a function, or a block of code.
8. Example
8
#include <stdio.h>int main () {
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d n", c);
f = 70.0/3.0;
printf("value of f : %f n", f);
return 0;
}
Output:
value of c : 30
value of f : 23.333334
Variable
Declaration and
Initialization