Basic Storage Classes in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. They precede the type that they modify. We have four different storage classes in a C program −

  • auto
  • register
  • static
  • extern

The auto Storage Class

The auto storage class is the default storage class for all local variables.

{
int mount;
auto int month;
}

The example above defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., local variables.

The register Storage Class

The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location).

{
register int miles;
}

The register should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.

The static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

In C programming, when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.

#include <stdio.h>

/* function declaration */
void func(void);

static int count = 5; /* global variable */

main() {

while(count--) {
func();
}

return 0;
}

/* function definition */
void func( void ) {

static int i = 5; /* local static variable */
i++;

printf("i is %d and count is %d
", i, count);
}

When the above code is compiled and executed, it produces the following result −

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

The extern Storage Class

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.

When you have multiple files and you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide the reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.

The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

First File: main.c

#include <stdio.h>

int count ;
extern void write_extern();

main() {

count = 5;
write_extern();
}

Second File: support.c 

#include <stdio.h>

extern int count;

void write_extern(void) {
printf("count is %d
", count);
}

Here, extern is being used to declare count in the second file, where as it has its definition in the first file, main.c. Now, compile these two files as follows −

$gcc main.c support.c

It will produce the executable program a.out. When this program is executed, it produces the following result −

The document Basic Storage Classes in C Programming | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Basic Storage Classes in C Programming - Programming and Data Structures - Computer Science Engineering (CSE)

1. What are the different storage classes in C programming?
Ans. In C programming, there are four basic storage classes: 1. Automatic Storage Class: - Variables declared inside a function without any storage class specifier. - They have a local scope and are automatically destroyed when the function exits. - The keyword "auto" is used to explicitly specify this storage class, but it is optional. 2. Register Storage Class: - Variables declared with the "register" keyword. - They are stored in the CPU registers for faster access. - The compiler decides whether a variable should be stored in a register or memory. 3. Static Storage Class: - Variables declared with the "static" keyword. - They have a local scope like automatic variables but retain their values across function calls. - Static variables are initialized only once and persist throughout the program's execution. 4. External Storage Class: - Variables declared outside of any function or block with the "extern" keyword. - They have a global scope and can be accessed by multiple source files. - External variables are typically used for sharing data between different functions or files.
2. What is the purpose of the automatic storage class in C programming?
Ans. The automatic storage class in C programming is used for declaring variables within a function without any storage class specifier. The purpose of the automatic storage class is to create variables that have a local scope and are automatically destroyed when the function exits. Variables declared with the automatic storage class are typically used for temporary storage within a function. They are created when the function is called and destroyed when the function returns. These variables are not accessible outside the function in which they are declared. The keyword "auto" can be used explicitly to specify the automatic storage class, but it is optional. If no storage class specifier is provided, the variable is assumed to have automatic storage class by default.
3. How does the register storage class work in C programming?
Ans. The register storage class in C programming is used to declare variables that are stored in CPU registers instead of memory. The purpose of using the register storage class is to optimize the access and manipulation of frequently used variables. By declaring a variable with the "register" keyword, the programmer suggests to the compiler that the variable should be stored in a register. However, it is ultimately the compiler's decision whether to honor this suggestion or not. The compiler takes into consideration factors such as register availability and the variable's usage pattern. Variables declared with the register storage class have faster access times compared to variables stored in memory. However, not all variables can be stored in registers. Variables that are too large or have their address taken cannot be stored in registers. It is important to note that using the register storage class does not guarantee that the variable will be stored in a register. It is merely a suggestion to the compiler for optimization.
4. What are the characteristics of static storage class variables in C programming?
Ans. Static storage class variables in C programming have the following characteristics: 1. Retention of Value: - Static variables retain their values across multiple function calls. - Once initialized, they persist throughout the execution of the program. - This allows static variables to maintain their values between different function invocations. 2. Local Scope: - Static variables have a local scope, similar to automatic variables. - They are only accessible within the block or function in which they are declared. 3. Default Initialization: - Static variables are initialized only once, at the time of declaration. - If no explicit initialization is provided, static variables are initialized to zero by default. 4. Lifetime: - Static variables exist for the entire duration of the program. - They are created when the program starts and destroyed when the program terminates. Static variables are commonly used when a variable needs to retain its value between function calls or when sharing data within a function but not with other functions.
5. How are external storage class variables used for sharing data between multiple source files in C programming?
Ans. External storage class variables in C programming are used for sharing data between multiple source files. They have a global scope, meaning they can be accessed by any function within the same program. To use an external variable, it must be declared outside of any function or block using the "extern" keyword. This declaration indicates that the variable is defined in another source file. In one source file, the external variable is defined without the "extern" keyword. This creates the actual storage for the variable. In other source files where the variable needs to be accessed, it is declared as "extern" to inform the compiler that the variable is defined elsewhere. By using external storage class variables, it becomes possible to share data between different functions or files. This allows for modular programming and the separation of concerns. However, it is important to ensure proper synchronization and avoid conflicts when modifying shared variables from multiple sources.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

MCQs

,

Semester Notes

,

Previous Year Questions with Solutions

,

Extra Questions

,

Objective type Questions

,

Viva Questions

,

ppt

,

Basic Storage Classes in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Basic Storage Classes in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Summary

,

Basic Storage Classes in C Programming | Programming and Data Structures - Computer Science Engineering (CSE)

,

Sample Paper

,

practice quizzes

,

pdf

,

shortcuts and tricks

,

Important questions

,

Exam

,

past year papers

,

study material

,

video lectures

,

Free

,

mock tests for examination

;