The different memory locations where data resides are called objects. To access such an object it must have a name, and such a named object is called a
. The information stored inside an object is called as a value. Of course the machine itself typically has no idea how to interpret the information it contains, but the program certainly needs to know. For this reason a variable has a specific type. And it’s this type that constrains the use of that variable, such that only operations appropriate to the type may be performed.
Any variable in C language acts like a container of data. C variables can be changed as per the requirement of the user and can be represented by any datatype like int, char, float, etc. C language variables are categorized into three groups.
- Variables which stores only one value at a time.
- Variables which stores multiple values of same data type.
- Variables which stores multiple values of different data type.
Computers don’t care about variable names, but those names are needed to refer the underlying objects like in local variables, function parameters, and so on.
- Variable names can consist of
letters (all alphabets a to z & A to Z) and
digits (all digits 0 to 9) but must begin with a letter only.
- Special characters such as colon : , semicolon ;, period ., underscore _, ampersand & etc. can also be used.
- Variable names are
case sensitive with lowercase letters typically used for variable names, and uppercase for constants.
- C reserves a few keywords and these cannot be used as variable names.
The C variables declared inside a program outside the functions are called as global variables.Global variable can be accessed even in the sub functions.
The C variables declared in a block of code are called as local variables.Local variables cannot be used in sub programming sections.
One can access environmental variables from anywhere in a C program without declaration and initialization in an application or a C-program.
int m = 3, n = 4; //global variables
int a = 1, b = 2; //local variables
printf(“All variables are accessed from main function”);
printf(“\nAll variables are accessed from test function”);
printf(“\nvalues: m=%d:n=%d:a=%d:b=%d”, m,n);
Scope states where a variable has its existence. If the scope rules are not followed, they are not accessible outside that program.
There are 3 places for declaring the C- variables:
1)Inside the function definition section which are also called as “formal parameters“.
2)Inside the block which are called as “local variables“.
3)Inside the program but outside of all blocks called as “global variables“.
Scope of Formal parameters:
The formal parameters can be used in functions.
- Formal parameters lie in function definition section.
- The formal parameters are local to the definition section and can be accessed by the main function of the program.
The scope of the local variable is within the block or function only.
- Local variables are not accessed by other blocks.
- Even if the variable name matches with the variable names outside the block, local block value cannot be taken outside the block.
Scope of Global Variables
The scope of the global variable is throughout the program.
- Global variables can be used anywhere in the program.
- Once the global variable value is changed, the block which uses it, also need to change the variable value.
- These variable values can be even used by other programs also.
Declaration of variables in C
Variables must be declared in the C program before to use.
- While declaration, memory space is not allotted for the variables.
- Value can be declared many times in the program.
Initialization of variables in C
The values are assigned to the C variables.
- Initialization allots the memory space as per the datatype given.
- Value is declared only once.
extern int a; //Declaration of variable a
int a=5; //Initialization of variable a
- Global variables can be declared anywhere.
- Local variables cannot be used outside of the scope.
- Every variable has to be initialized.
C variables must be declared with a type at the beginning of the program.