×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Storage classes in C programming language

Learn: In this article we learn about the different storing classes used in C programming language.
Submitted by Abhishek Kataria, on April 30, 2018

Storage classes Overview in C

A variable's storage class tells us the following,

  1. Where the variables would be stored?
  2. What will be the initial of the variable, if the initial value is not specifically assigned? (i.e. the default initial value).
  3. What is the scope of the variables, i.e. in which part of the program of the functions the value of the variable would be available?
  4. What is the life of the variable, i.e. how long the variable exists?

Types of Storage Classes in C

There are four classes in C programming language,

  1. Automatic storage classes
  2. Register storage classes
  3. Static storage classes
  4. External storage classes

1) Automatic storage classes

The keyword auto is used to declare variable of automatic storage class. (keyword auto is optional).

Syntax

    auto int a;
    int a;
StorageMemory
Default initial value Unpredictable value
Scope Local to the block in which the variable is defined.
Life Control remains within the block in which the variable is defined.

Example: To display the default values

#include <stdio.h>

int main ()
{
	auto int i,j;
	
	printf("\n%d %d",i,j);
	
	return 0;
}

NOTE: In the output two garbage values will be displayed as automatic variable by default store garbage values.

2) Register storage classes

The keyword register is used to declare a variable of the register storage class.

Syntax

 register int a;
Storage CPU Register
Default initial value Garbage value
Scope Local to the block in which the variable is defined.
Life Control remains within the block in which the variable is defined.

Example:

#include <stdio.h>

int main()
{
	register int i;

	for(i=1;i<=100;i++);
		printf("%d",i);

	return 0;
}

Output

    101

NOTE: A variable stored in CPU register can always be accessed faster than the one which is stored in memory.

We cannot use register storage class for all types of variables.

Example: register double a; ,register float c; etc.

3) Static storage classes

The keyword static is used to declare variables of static storage class.

Syntax

 static int i;
Storage Memory
Default initial value 0
Scope Local to the block in which the variable is defined.
Life Value of the variable remains b/w different function calls.

Example:

#include <stdio.h>

void  abc()
{
	static int a=10;
	printf("%d\n",a);
	a=a+10;
}

int main()
{
	abc();
	abc();
	abc();
}

Output

    10
    20
    30

NOTE: We should avoid using static variables unless we really need them. Because their value are kept in memory when the variables are not active, which means they take up space in memory that could otherwise be used by other variables.


4) External storage classes

The keyword extern is used to declare the variables of external storage class.

In this the variable declared without keyword but it should be defined above the function(outside).

Syntax

 extern int i;
Storage Memory
Default initial value 0
Scope Global
Life As long as the program execution does not come to an end.

Example:

#include <stdio.h>

/*Global variable*/
int a=10;   

void abc()
{
	printf("%d\n",a);
	a=a+10;
}

int main()
{
	a=a+5;
	printf("%d\n",a);
 
	a=a+20;
	abc();
	printf("%d\n",a);
	
	abc();
	a=a+20;
	
	abc();
	printf("%d\n",a);
	
	return 0;
}

Output

    15
    35
    45
    45
    75
    85

NOTE: Mostly in many cases preference is given to a local variable.




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.