Introduction
Storage classes in C help determine important properties of variables, such as scope, lifetime, visibility, and storage behavior. The main storage classes are auto, register, static, and extern. In this chapter, you will practice each storage class with simple programs and understand when variables are created, where they can be accessed, and how long their values remain available. Storage Classes in C Practice questions with solutions to help you understand the concepts.
Q1. Use the auto Storage Class
Problem Statement
Create a local integer variable using the auto storage class and display its value.
C Program
#include <stdio.h>
int main()
{
auto int number = 100;
printf("Number = %d", number);
return 0;
}
Sample Output
Number = 100
Explanation
The auto keyword is used for local variables.
auto int number = 100;
For a local variable, auto is actually the default storage class. Therefore, this:
auto int number = 100;
is equivalent to:
int number = 100;
The variable exists while execution is inside its block.
Concepts Covered
auto- Local variables
- Variable scope
- Default storage class
Q2. Understand the Default auto Behavior
Problem Statement
Create two local variables, one explicitly declared with auto and another without specifying a storage class. Display both values.
C Program
#include <stdio.h>
int main()
{
auto int a = 10;
int b = 20;
printf("a = %d\n", a);
printf("b = %d", b);
return 0;
}
Sample Output
a = 10
b = 20
Explanation
These two declarations are equivalent in terms of storage class:
auto int a = 10;
and:
int b = 20;
Local variables declared without a storage-class specifier have automatic storage duration by default.
The auto keyword is therefore rarely necessary in normal C programs.
Concepts Covered
- Automatic storage duration
auto- Local variables
- Variable declaration
Q3. Use static Inside a Function
Problem Statement
Create a function containing a static variable. Call the function three times and observe whether the variable remembers its previous value.
C Program
#include <stdio.h>
void counter()
{
static int count = 0;
count++;
printf("Count = %d\n", count);
}
int main()
{
counter();
counter();
counter();
return 0;
}
Sample Output
Count = 1
Count = 2
Count = 3
Explanation
The important line is:
static int count = 0;
A normal local variable is created when the function is entered and its automatic lifetime ends when the function returns.
A static local variable is different.
Its value is preserved between function calls.
The first call:
count = 1
The second call:
count = 2
The third call:
count = 3
The variable is initialized only once.
Concepts Covered
static- Static local variable
- Function calls
- Persistent value
Q4. Create a Function Call Counter
Problem Statement
Write a program that counts how many times a function has been called using a static variable.
C Program
#include <stdio.h>
void showMessage()
{
static int calls = 0;
calls++;
printf("Function called %d time(s)\n", calls);
}
int main()
{
showMessage();
showMessage();
showMessage();
showMessage();
return 0;
}
Sample Output
Function called 1 time(s)
Function called 2 time(s)
Function called 3 time(s)
Function called 4 time(s)
Explanation
The variable:
static int calls = 0;
belongs to the function’s local scope, but its stored value remains between function calls.
So each time:
calls++;
runs, the previous value is increased.
This is a common beginner-friendly example of why static can be useful.
Concepts Covered
- Static local variable
- Variable lifetime
- Function calls
- Increment operator
Q5. Compare Normal and Static Local Variables
Problem Statement
Create one normal local variable and one static local variable inside a function. Call the function multiple times and observe the difference.
C Program
#include <stdio.h>
void test()
{
int normal = 0;
static int staticValue = 0;
normal++;
staticValue++;
printf("Normal = %d, Static = %d\n", normal, staticValue);
}
int main()
{
test();
test();
test();
return 0;
}
Sample Output
Normal = 1, Static = 1
Normal = 1, Static = 2
Normal = 1, Static = 3
Explanation
The normal variable:
int normal = 0;
is initialized each time the function is called.
Therefore, it becomes:
1
1
1
The static variable:
static int staticValue = 0;
keeps its previous value.
Therefore, it becomes:
1
2
3
This is one of the easiest ways to understand the practical difference between automatic and static local variables.
Concepts Covered
- Automatic local variable
- Static local variable
- Variable lifetime
- Function scope
Q6. Use static with a Global Variable
Problem Statement
Create a global static variable and use it inside the same source file.
C Program
#include <stdio.h>
static int number = 100;
void display()
{
printf("Number = %d\n", number);
}
int main()
{
display();
return 0;
}
Sample Output
Number = 100
Explanation
Here:
static int number = 100;
is declared outside all functions.
A file-scope static variable has internal linkage. This means its name can be accessed from functions in the same source file, but it cannot be accessed by name from another source file using extern.
In this program, display() can access number because both are in the same source file.
Concepts Covered
- File-scope
static - Global variables
- Internal linkage
- Function access
Q7. Use the register Storage Class
Problem Statement
Create a variable using the register storage-class specifier and use it in a loop.
C Program
#include <stdio.h>
int main()
{
register int i;
for (i = 1; i <= 5; i++)
{
printf("%d\n", i);
}
return 0;
}
Sample Output
1
2
3
4
5
Explanation
The declaration:
register int i;
uses the register storage-class specifier.
Historically, register was intended to request that the implementation keep a frequently used variable in a CPU register for potentially faster access.
Modern C compilers generally make their own optimization decisions, so register is rarely needed.
One important rule is that you cannot apply the address-of operator & to an object declared with the register storage-class specifier.
For example, this should not be done:
register int i;
printf("%p", (void *)&i);
Concepts Covered
register- Loop variable
- Storage-class specifier
- Compiler optimization
Q8. Understand extern with a Global Variable
Problem Statement
Create a global variable and use the extern declaration to access it from a function.
C Program
#include <stdio.h>
int number = 500;
void display()
{
extern int number;
printf("Number = %d", number);
}
int main()
{
display();
return 0;
}
Sample Output
Number = 500
Explanation
The global variable is defined here:
int number = 500;
Inside display(), we write:
extern int number;
This tells the compiler that number is defined elsewhere in the program.
In this example, the definition happens earlier in the same source file.
The important point is that extern is primarily about declaring an object whose definition is provided elsewhere, and it is commonly used when multiple source files share global variables.
Concepts Covered
extern- Global variables
- External linkage
- Variable declaration
Q9. Use extern with Two C Files
Problem Statement
Create a global variable in one C source file and access it from another source file using extern.
File 1: main.c
#include <stdio.h>
extern int marks;
int main()
{
printf("Marks = %d", marks);
return 0;
}
File 2: data.c
int marks = 85;
Compile
If using GCC:
gcc main.c data.c -o program
Then run:
program
Sample Output
Marks = 85
Explanation
The actual variable is defined in data.c:
int marks = 85;
In main.c, we only declare that the variable exists:
extern int marks;
During the linking stage, the compiler toolchain connects the reference in main.c with the definition in data.c.
This is one of the main practical uses of extern.
Concepts Covered
extern- Multiple source files
- Global variable
- Linker
- External linkage
Q10. Combine static, extern, and Functions
Problem Statement
Create two source files. Keep one global variable private to its source file using static, and create another global variable that can be accessed from another source file using extern.
File 1: main.c
#include <stdio.h>
extern int publicNumber;
int main()
{
printf("Public Number = %d\n", publicNumber);
return 0;
}
File 2: data.c
int publicNumber = 100;
static int privateNumber = 200;
Compile
gcc main.c data.c -o program
Sample Output
Public Number = 100
Explanation
This variable:
int publicNumber = 100;
has external linkage by default, so another source file can refer to it using:
extern int publicNumber;
But:
static int privateNumber = 200;
has internal linkage.
Therefore, privateNumber cannot be accessed by name from main.c.
This demonstrates an important difference:
int variable
↓
Can have external linkage
static int variable
↓
Internal linkage at file scope
Concepts Covered
staticextern- Multiple source files
- Internal linkage
- External linkage
Key Takeaways
- C provides four commonly discussed storage-class specifiers:
auto,register,static, andextern. autois the default storage class for ordinary local variables.- Automatic variables have automatic storage duration.
registerrequests register storage but does not guarantee it.- You cannot apply
&to an object declared with theregisterstorage-class specifier. - A local
staticvariable retains its value between function calls. - A file-scope
staticvariable has internal linkage. externis used to declare an object that is defined elsewhere.externis particularly useful when multiple.cfiles share global variables.- Scope and lifetime are different concepts.
- Storage classes become especially important when working with larger multi-file C programs.
FAQs
1. What are storage classes in C?
Storage classes are used to specify properties related to variables and functions, including storage duration, scope, and linkage. Common storage-class specifiers include auto, register, static, and extern.
2. What is the default storage class of a local variable in C?
An ordinary local variable has automatic storage duration by default. Writing auto explicitly is usually unnecessary.
int x = 10;
and:
auto int x = 10;
have the same basic automatic-storage behavior.
3. What is the use of static in C?
static has different effects depending on where it is used. A local static variable retains its value between function calls. A file-scope static variable has internal linkage and is limited to that source file.
4. What is the use of extern in C?
extern declares an object whose definition is provided elsewhere. It is commonly used to share global variables between multiple C source files.
5. Does register guarantee that a variable will be stored in a CPU register?
No. register is a request to the implementation. Modern compilers can make their own optimization decisions.
6. What is the difference between static and extern?
A file-scope static declaration gives a variable internal linkage, restricting access by name to that source file. extern is commonly used to refer to an object with external linkage that is defined elsewhere.
7. Why is static useful inside a function?
A static local variable keeps its value after the function returns. This makes it useful for tasks such as maintaining a function call counter or preserving state between calls.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
