Share | Tweet |
---|
Subscribe here to enable complete access to all technical articles, online tests and projects.
This article will help you to gain required knowledge to successfully complete online tests, technical projects which are required to complete online courses and earn course completion certificates for software jobs.
Storage class determines the scope, lifetime, storage location and initial value of a variable. There are four storage classes in C such as Automatic, Static, Register, External
A variable of each storage class can be declared by writing a storage specifier in front of the variable. At most one storage specifier can be written in front of a variable. The storage specifiers for each storage class is as follows:
Variables with automatic storage class are defined inside a function and are also called local variables. Auto/local variables have the following rules.
A variable declared outside all the functions is called a global variable. Global variables have the following rules.
To demonstrate the auto/local, global variable features, let's write a program as shown below.
#include <stdio.h>
int autoFunc(int );
int glbvar = 10;
int glbvar1;
int main() {
printf("glbvar:%d,glbvar1:%d\n", glbvar, glbvar1);
int autovar = 20;
int res;
printf("res is:%d\n", res);
res = autoFunc(autovar);
printf("res is:%d\n", res);
return 0;
}
int autoFunc(int autovar)
{
printf("0th block:%d\n", autovar);
{
auto int autovar = 30;
printf("Ist block:%d\n", autovar);
{
auto int autovar = 40;
printf("2nd block:%d\n", autovar);
}
}
glbvar += autovar;
return glbvar;
}
Above program will produce the following output. $ gcc Listing1.c $ ./a.out glbvar:10,glbvar1:0 res is:0 0th block:20 Ist block:30 2nd block:40 res is:30 $
Let's analyze the above program. The following are the points to observe from the above program.
A static variable remains in memory while the program is running in contrast to a normal or auto variable which is destroyed when a function call where the variable was declared is over. Following are the features of a static variable:
Let's write a program to demonstrate the use of static variables
#include <stdio.h>
static int p;
int f(void) {
static int x = 0;
x++;
return x;
}
int y(void) {
int x = 0;
x++;
return x;
}
int main()
{
int j;
printf("Static variable x:%d\n", p);
for (j = 0; j < 5; j++) {
printf("Value of f(): %d\n", f());
}
for (j = 0; j < 5; j++) {
printf("Value of y(): %d\n", y());
}
return 0;
}
Above program will produce the following output. $ gcc prog1.c $ ./a.out Static variable x:0 Value of f(): 1 Value of f(): 2 Value of f(): 3 Value of f(): 4 Value of f(): 5 Value of y(): 1 Value of y(): 1 Value of y(): 1 Value of y(): 1 Value of y(): 1 pi@raspberrypi:~/try $
Let's analyze the above program. Here we have two functions f and y. Function f is having a static int variable x where as function y is having a non-static variable x. You can see in the output that though we called both the functions 5 times, output of function f keeps incrementing on successive calls where as the output of function y remains always at 1. This is because the static variable x declared inside function f is not reinitilized to 0 where as in function y variable x is reinitialized to 0 on successive calls.
Also when we printed an uninitialized static variable p, inside main function, compiler automatically initilized to 0. So when we printed it inside main function, it prints as 0.
Let's write another program and compile along with the above program as shown below.
#include <stdio.h>
extern int p;
int function()
{
printf("printing static var p:%d\n", p);
}
When prog1.c and prog2.c are compiled together, the complier will give the following error. pi@raspberrypi:~/try $ gcc prog1.c prog2.c /tmp/cciYesJG.o: In function `function': prog2.c:(.text+0x28): undefined reference to `p' collect2: error: ld returned 1 exit status pi@raspberrypi:~/try $
If you see the above program output, it shows that the variable p which is declared as static in prog1.c could not be accessed in the prog2.c even though extern keyword is used to access it from prog1.c file. So it confirms that static variable can be accessed only in the file where it is declared.
Now remove the static storage specifier from variable p in prog1.c. You should be able to compile prog1.c and prog2.c together. Compiler will not give any error
Share | Tweet |
---|
SPI protocol theory of operation |
I2C programming in embedded C |
Microcontroller fundamentals in a nutshell |
How UART protocol works? |
What is pwm and how it works |
Python Strings |
Python Lists |
Search more C examples |
Search more C MCQs |
Linux software development |
C programming & applications |
Embedded systems development |
Linux OS Internals |
Git Version Control |
Linux OS Commands |
Linux IPC Mechanisms |
Linux Processes and Threads |
LCM and HCF of numbers |
Time and Distance |
Series completion |
SPI Protocol & Applications |
I2C Protocol & Applications |
Microcontrollers, Tools & Peripherals |
UART Protocol & Applications |