C Language Storage Classes

Storage classes are used to know the scope and lifetime of a variable. There are 2 types of storage classes.
1. Local
2. Global
Below are different storage class specifiers:
1. auto
2. register
3. static
4. extern
Before looking into storage classes, we shall discuss about variables and how they behave in different blocks. A block in C, are set of statements those are written inside “{ … }” braces.
Below are different types of blocks available in C:
1. Function block.
2. File Block.
3. Global Block.
4. Nested block.
These blocks define the scope, lifetime and linkage of variables. In C we can use the same variable names in different blocks.

1. automatic variables:

Any variables declared inside a function will be of automatic storage class. “auto” is the keyword used to declare a variable as a automatic storage class.
1. Automatic variables are stores in the stack segment.
2. Their scope is with in the block they are defined.
3. Their lifetime is till the execution of the function.
4. Their default value will be garbage if not defined.
Example:
auto int i = 10;
int j = 20;

Example:

#include<stdio.h>
int main(int argc, char const *argv[])
{
	
	int num = 10;

	{
		int num = 20;
		printf("The num value inside nested block = %d \n", num );
	}
		printf("The num value inside main block = %d \n", num );

	return 0;
}

Output:

The num value inside nested block = 20
The num value inside main block = 10

2. Register Specifier:

“register” is the keyword used to declare a variable as a register storage class. A variable declared as a register, is stored in the computer register memory. However, this is just a request to the compiler to store the value in a register and is not guaranteed that it will be stored there.

1. Register variables are stores in the computer register if available.

2. Their scope is with in the block they are defined.

3. Their lifetime is till the execution of the block.

4. Their default value will be garbage if not defined.

Example:

register int i = 10;

3. Static Specifier:

“static” is the keyword used to declare a variable as a static storage class.

1. static variables are stores in the data segment.

2. Their lifetime is till the execution of the program.

3. Their default value will be zero if not defined.

4. If a local variable is declared as static, then that variable will be maintaining its value between the function calls.

5. If a global variable is declared as static, then that variable scope is that of the file. That variable cannot be accessed from other files.

Example:

static int i = 10;

Program example:
 #include<stdio.h>

static int num;

void func_1()
{
	static int called= 1;
	printf("This function is called %d times\n",called );
	called++;
}

int main(int argc, char const *argv[])
{
	printf("The global value of static variable is %d\n", num);	

	func_1();
	func_1();
	func_1();
	func_1();

	return 0;
}

Output:

The global value of static variable is 0
This function is called 1 times
This function is called 2 times
This function is called 3 times
This function is called 4 times

4. Extern Specifier:

“extern” is the keyword used to declare a variable as an extern. It is an indication for the programmer that the variable is defined in other place, and it is using a reference. Initialization of the variable cannot be done, because it has been defined and initialized in other file.

1. extern variables are stores in the data segment.

2. Their lifetime is till the execution of the program.

3. Their default value will be zero if not defined.

4. The scope is in the file the variable has been imported.

Example:

extern int i = 10;

Programming example:

1.c
#include<stdio.h>

extern void	func_1();

int num;

int main(int argc, char const *argv[])
{
	printf("In the main function, global value of variable is %d\n", num);	
	func_1();
	
	return 0;
}
2.c
#include<stdio.h>

extern int num;

int func_1()
{
	printf("In the file 2, in finc_1 function, value of extern variable is %d\n", num);	

	// initialization is wrong, but you can change the value of that variable.
	num += 20;
	printf("In the file 2, in finc_1 function, value of extern variable after changing value is %d\n", num);	


	return 0;
}
Compiling both of the programs:
gcc 1.c 2.c

Output:

./a.out

In the main function, global value of variable is 0
In the file 2, in finc_1 function, value of extern variable is 0
In the file 2, in finc_1 function, value of extern variable after changing value is 20
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *