Coding with C is highly centered upon using variables in every program. Those are the key aspects of C programming. Every variable in C has two properties; type and storage classes. Among them, the type refers to the data type of the variable, and storage classes in C determine the scope, lifetime, and visibility of the variable. Storage classes in C are used to find the lifetime, visibility, memory location, and initial value of a variable.
In this blog post, we will have a detailed look at the storage classes in C, its types, and how its characteristics influence the output of the program with some programming examples. A storage class in C is used to represent the information of a variable.
Storage Classes In C With Examples Of Use
Any storage class actually is representative of visibility and location of any variable. This tells users what portion of a code is accessible as a variable. Storage class for C can help describe the following things:
- A variable scope
- Location for variable storage
- Initialized value for variable
- Variable lifetime
- Users accessing a variable
Overall, a storage class stands representative of information about any variable. This is the best way to explain storage classes in c with examples.
Also, Check out our free courses
What Are Storage Classes In C?
Storage classes in C allocate the storage area of a variable that will be retained in the memory. They are stored in the RAM of a system. Apart from the storage space, they determine the scope of a variable. Variables in the C program are stored in a physical location in the random memory, which is mainly the device’s memory and CPU registers.
Storage classes in C also define the lifetime of the variable and term it as ‘local’ or ‘global’. Storage classes are also useful to define the scope or visibility, and the initial value of the variable. There are primarily four storage classes in C, viz. automatic, register, static, and external. We will discuss each one by one further.
Check out upGrad’s Full Stack Development Bootcamp
Get Software Development Course from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
How Storage Classes In C Are declared?
The four storage classes in C are declared in a block or program with the storage class specifiers, auto, register, extern, static. There is one more storage class specifier, ‘typedef’ used in the syntactic form, and does not reserve storage. The specifiers instruct the compiler on storing the variables. The external storage class in C tell the compiler that variable defined is declared with external linkage.
There is a key difference between defining and declaring a variable. Defining a variable is about allocating the memory for the variable and declaring it means initializing it with a value.
Check out Java Bootcamp from upGrad
storage_class_specifier data_type variable_name;
Special Case: When no storage class specifier is declared or defined in a program
There is at least one storage class specifier is given in the variable declaration. But, in case, there is no storage class specifier specified, the following rules are followed:
1. Variables declared within a function are considered as auto.
2. Functions declared within a function are considered as an extern.
3. Variables and functions declared outside a function are considered static, with external linkage.
upGrad’s Exclusive Software and Tech Webinar for you –
SAAS Business – What is So Different?
What are the Types of Storage Classes in C?
There are four storage classes in C, let’s have a look at them:
1. Automatic Storage Classes in C
Every variable defined in a function or block belongs to automatic storage class by default if there is no storage class mentioned. The variables of a function or block belong to the automatic storage class are declared with the auto specifier. Variables under auto in C are local to the block where they are defined and get discarded outside the block.
A Simple Program Showing Automatic Storage Classes:
int main( )
auto int i = 11;
auto int i = 22;
auto int i = 33;
printf ( “%d “, i);
printf ( “%d “, i);
printf( “%d”, i);
The output of the Program:
3 2 1
In the above program, there is three times the variable i is declared. Variables with the same name can be defined in different blocks. Thus, this program will compile and execute successfully without any error. The function ‘printf’ in the innermost block will print 3 and the variable i in this block will be destroyed after the block ends.
The next one, the second outer block prints 2 which is then succeeded by the outer block which prints 1. The automatic variables are initialized properly; else you will get undefined values as the compiler does not give them an initial value.
Explore our Popular Software Engineering Courses
2. Register Storage Classes in C
The variables belonging to a register storage class are equivalent to auto in C but are stored in CPU registers and not in the memory, hence the name. They are the ones accessed frequently. The register specifier is used to declare the variable of the register storage class. Variables of a register storage class are local to the block where they are defined and destroyed when the block ends.
A Simple Program Showing Register Storage Classes:
register int i = 10;
int *p = &i; //error: address of register variable requested
printf(“Value of i: %d”, *p);
printf(“Address of i: %u”, p);
In the above program, the code tries to get the address of variable i into the pointer variable p but as i is declared as a register variable, the code won’t compile and will display the error ” Error: address of register variable requested”.
Only certain types of variables are placed into registers. Register variables are not given an initial value by the compiler.
3. Static Storage Classes in C
The visibility of static variables is zero outside their function or file, but their values are maintained between calls. The variables with static storage class are declared with the static specifier. Static variables are within a function or file. The static specifier works differently with local and global variables.
A static storage class will instruct a compiler for keeping the local variable existing during the span of a program in place of creating and then destroying the same every time the scope arises around it. Thus, making all local variables static will allow the maintenance of the values between all function calls.
A static modifier gets applied to all global variables. Once done, this causes that particular variable scope restriction to a file where the same is declared.
The static specifier in programming works differently with both local and global variables. Take a look below to understand the static storage class in c.
Simple Programs Showing Static Storage Classes with Local and Global Variables:
i. Local Variable
static int i;
static int i = 1;
printf(“%d “, i);
The output of the Program:
When a local variable is defined by a static specifier, inside a function or block, permanent storage space is created in the compiler. The static local variable is visible to the function or block where it is specified and retains its value between the function calls. In the above program, the static variable i is defined at two places in two blocks inside the staticDemo()function. staticDemo() is called two in the main function. In the next call, the static variables retain their old values and need not be initialized again.
ii. Global Variable
static int gInt = 1;
static void staticDemo()
static int i;
printf(“%d “, i);
The output of the Program:
Static variables need to be initialized only once in a program and they are retained throughout the lifetime. They have a default initial value of zero.
When a global variable or function is defined by a static specifier, then that variable or function is known only to the file in which it is defined. For a global variable, other file routines cannot access and alter its contents as a static global variable has internal linkage. In the above program, the static global variable globalInt and a static function staticDemo(), are defined as static and they cannot be used outside the C file.
In-Demand Software Development Skills
4. External Storage Classes in C
External storage class variables or functions are declared by the ‘extern’ specifier. When a variable is declared with extern specifier, no storage is allotted to the variable and it is assumed that it has been already defined elsewhere in the program. With an extern specifier, the variable is not initialized. The reason why extern is used to specify a variable in a program to declare it with external linkage.
A Simple Program Showing External Storage Classes:
extern int i;
printf(“i: %d”, i);
int i = 1;
In the above C program, if extern int i is removed, there will be an error “Undeclared identifier ‘i’ because the variable i is defined after being used in printf. The extern specifier instructs the compiler that variable i has been defined and is declared here.
If you change extern int i; to extern int i = 5; you will get an error “Redefinition of ‘i'” because the extern specifier does not initialize a variable.
Read our Popular Articles related to Software Development
A Quick Summary Of Various Storage Classes In C
Let us take a quick look at summing up the basic information about various storage classes in c.
- Any storage class in C can be used for representing any additional information about any variable.
- Storage class stands for the scope as well as the lifespan of the variable.
- Storage class also tells clearly who can access any variable and from what place.
- The four different storage classes in C program are auto, register, extern, and static.
- Storage class specifier for C language can be used for defining variables or functions as well as parameters.
- Auto can be used for any local variable that is defined within the block or function.
- Register gets used for storing a variable in the CPU registers instead of a memory location for ease of access.
- Static gets usage for global as well as local variables. Both have their use cases within the C program.
- Extern can be used for data sharing between multiple C files.
Advantages and disadvantages of C Storage Class
Let’s look at the advantages of The C Storage Class:
- Helps to determine the scope, visibility, lifetime of a variable.
- We can use more than one variable with the same name.
Some disadvantages of The C Storage Class:
- We cannot use more than one storage class for a single variable.
- Changing the program to meet different needs becomes difficult, as we have to keep in mind the scope and lifetime of the variable.
Explore Our Software Development Free Courses
|Blockchain Technology||React for Beginners||Core Java Basics|
This article details the concept of storage classes in C and tells you how its types differ from each other. When to use a particular storage class depends on the assignment and the scope, lifetime, and visibility of the variable you are dealing with.
If you are interested to learn more and need mentorship from industry experts, check out upGrad & IIIT Banglore’s PG Diploma in Full-Stack Software Development.
What is the difference between an Array and a Stack?
Storing and organising data is necessary so that data can be accessed easily when required. Stack and Array are the two most common ways to store data. A Stack is a data structure represented by a sequential collection of elements in a fixed order. On the other hand, the Array is a collection of data values called elements, each of which is identified by an indexed array. A Stack can be implemented using an Array, while an Array cannot be implemented using a Stack.
What is the difference between Structure and Union?
The C programming language has many built-in data types. Users can create their own custom data types in 5 ways: bit-field, structure, union, typedef, and enumeration. Structure and Union are both user-defined data types. A structure is a user-defined, custom data type in C language which allows combining data items of different types under a single unit. A union is a data type that allows storing different data types in the same memory location. Several members can initialise at once in a structure, while only the first union member can be initialised. The keyword `struct’ is used to define a structure, while the keyword `union’ is used to define a union. Also, altering the value of one member in a structure will not affect the members of other structures. On the contrary, altering the value of a member of the Union will alter other members' values.
Which storage class helps in faster execution?
Storage classes are used to determine any given variable's memory, location, initial value, and visibility. Storage classes allocate the storage area of a variable to be retained in the memory. Although there are 5 different types of storage classes in C, the register storage class helps in faster execution. Register has more immediate access as the variables are stored in CPU registers rather than the RAM. The variables declared using register storage have no default value and are usually declared in the beginning of the program.