In C we have the following types of variables:
Local variables:
Local variables are variables that are defined inside a function or a block of code. The block of code is referred to some kind of loop or a condition. These variables are visible/accessible only inside the function or that block of code and their lifetime or exist until that function is under execution.
Once the function or that block of code is over, even those variables go out of existence. We can use such variables inside that function or block the code and cannot be used once that function execution is over.
Program scope/Global variable:
This variable is accessible throughout the program and exists until the program terminates.
Static variables: A static variable can either be:
- Static Local Variable
- Static Global Variable
Static Local variable:
This is similar to local variables inside a function or a block of code. The major difference between a static local variable and a normal local variable (non-static) is that the lifetime of the local variable is until the function or a block of code is under execution. Once the function execution is over, the local variable also goes out of existence, and next time when the control reaches the function altogether a new instance of variable is created again.
However, in the case of a static local variable, the lifetime of the variable is until the program terminates. They maintain their values during multiple invocations of the function.
The static variable is always initialized by 0 by default whereas the local non-static variable may be initialized by garbage value.
Static global variable
This is also a global variable and such variables are accessible throughout the program and exist until the program terminates.
The key difference between global scope and file scope is that variables with file scope cannot be used outside that file whereas variables with program scope or global scope can be used outside that file.
Sample Program:
Local variable | Static Local variable |
#include <stdio.h> void fun (int val) { int local_var = 0; local_var = local_var + val; printf (“\n The sum is %d\n”, local_var); } int main() { int val = 5; int i = 0; for ( i =0 ; i <3; i++) fun (val); return 0; } | #include <stdio.h> void fun (int val) { static int local_var = 0; local_var = local_var + val; printf (“\n The sum is %d\n”, local_var ); } int main() { int val = 5; int i = 0; for ( i =0 ; i <3; i++) fun (val); return 0; } |
Output: The sum is 5 The sum is 5 The sum is 5 | Output: The sum is 5 The sum is 10 The sum is 15 |
In the above program, we can see that for local variables, the variable local_var is re-initialized to 0 every time the function is called, but when the same variable is made static, it does get initialized to 0 only for the first time. From the second call, it does maintain the last state, persists with the older value, unlike the non-static local value which loses its last state/value.
Extern variable:
Non-static global variables can be accessed outside the file. The possibility of accessing it outside the file is because of an extern keyword.
The extern keyword is used before a variable to inform the compiler that this variable is defined somewhere else in the program. The extern declaration does not allocate storage for variables.
header.h | a.c |
#include <stdio.h> int a = 10; | #include <stdio.h> #include “header.h” extern int a; int main() { a = a + 10; printf(“\n value of a is %d\n”, a); return 0; } |
Consider the above as two different source codes, one on the left as header.h, which is nothing but a header file where we have a variable of type int with value 10. Whereas the second file is the main.c file which contains the logic for our code.
Points to be noted:
- In the .c file, we have added/included an extra header file which is the other file in our case which is the header.h
- There is an extern variable “a”, which simply tells the compiler that this variable is defined somewhere else.
- So the compiler at the preprocessor stage will expand the included header file “header.h”.
- So now we can see that we are able to access the variable which is defined in another file with the help of the extern keyword.
Automatic Variables
- Any variable that is declared inside a function or a block is automatic by default. This is very much the same as that of the local variable. These contain garbage values until they are initialized.
- Example: auto int a;
Relevant Posts:
Categories: C Language
Leave a Reply