Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programming language

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

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.






Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.