C Programming • Structure and Union
C Programming / Types of Storage Classes — Exercises

Types of Storage Classes — Exercises

Practical 4 Structure and Union

Write a C program to declare a local variable using the auto storage class and display its value.

Practical / Solution

Storage Classes — Exercise 1

Problem

Write a C program to declare a local variable using the auto storage class and display its value.

Program

#include <stdio.h> int main() { auto int num = 25; // Display the auto variable printf("Value = %d\n", num); return 0; }

Explanation

The auto storage class is the default storage class for local variables. An automatic variable exists while the block in which it is declared is executing.

Expected Output

Value = 25

Storage Classes — Exercise 2

Problem

Write a C program to demonstrate the use of a local auto variable inside a function.

Program

#include <stdio.h> void display() { auto int value = 10; // Display local automatic variable printf("Value inside function = %d\n", value); } int main() { display(); return 0; }

Explanation

The auto variable is local to the function. Its scope is limited to that function block.

Expected Output

Value inside function = 10

Storage Classes — Exercise 3

Problem

Write a C program to demonstrate a register variable used as a loop counter.

Program

#include <stdio.h> int main() { register int i; // Use register variable as loop counter for (i = 1; i <= 5; i++) { printf("%d ", i); } return 0; }

Explanation

The register storage class is used to request that a frequently accessed local variable be kept in a CPU register when practical. The compiler decides whether the request can be honored.

Expected Output

1 2 3 4 5

Storage Classes — Exercise 4

Problem

Write a C program using a static local variable to demonstrate that its value is preserved between function calls.

Program

#include <stdio.h> void counter() { static int count = 0; // Increase the preserved value count++; printf("Count = %d\n", count); } int main() { // Call the function multiple times counter(); counter(); counter(); return 0; }

Explanation

A local static variable retains its value between function calls. Unlike an ordinary local variable, it is not reinitialized on every call.

Expected Output

Count = 1

Count = 2

Count = 3

Storage Classes — Exercise 5

Problem

Write a C program to compare an ordinary local variable with a static local variable by calling the same function three times.

Program

#include <stdio.h> void compareVariables() { int normal = 0; static int persistent = 0; // Increment both variables normal++; persistent++; printf("Normal = %d, Static = %d\n", normal, persistent); } int main() { // Call function repeatedly compareVariables(); compareVariables(); compareVariables(); return 0; }

Explanation

The ordinary local variable is initialized again on every function call, so its value remains 1. The static variable retains its previous value and therefore increases with every call.

Expected Output

Normal = 1, Static = 1

Normal = 1, Static = 2

Normal = 1, Static = 3

Storage Classes — Exercise 6

Problem

Write a C program to demonstrate an extern variable declared outside the function and accessed inside main().

Program

#include <stdio.h> int number = 50; int main() { extern int number; // Access the externally declared variable printf("Number = %d\n", number); return 0; }

Explanation

The extern declaration tells the compiler that the variable is defined elsewhere. Here, the actual definition of number is outside main().

Expected Output

Number = 50

Storage Classes — Exercise 7

Problem

Write a C program to use a global variable with an extern declaration inside a function and modify its value.

Program

#include <stdio.h> int value = 20; void update() { extern int value; // Modify the global variable value = value + 10; } int main() { printf("Before update = %d\n", value); update(); printf("After update = %d\n", value); return 0; }

Explanation

The extern declaration allows the function to refer to the global variable defined outside it. Therefore, the update is visible in main().

Expected Output

Before update = 20

After update = 30

Storage Classes — Exercise 8

Problem

Write a C program to demonstrate a static variable used to count how many times a function has been called.

Program

#include <stdio.h> void functionCallCount() { static int count = 0; // Preserve and increment the call count count++; printf("Function called %d time(s).\n", count); } int main() { functionCallCount(); functionCallCount(); functionCallCount(); functionCallCount(); return 0; }

Explanation

The static variable retains its value after the function returns. This makes it useful for maintaining state across multiple calls.

Expected Output

Function called 1 time(s).

Function called 2 time(s).

Function called 3 time(s).

Function called 4 time(s).

Storage Classes — Exercise 9

Problem

Write a C program to demonstrate the difference between a normal local variable and a static local variable using repeated function calls.

Program

#include <stdio.h> void demonstrate() { int normal = 10; static int persistent = 10; // Modify both variables normal = normal + 5; persistent = persistent + 5; printf("Normal = %d, Static = %d\n", normal, persistent); } int main() { demonstrate(); demonstrate(); demonstrate(); return 0; }

Explanation

The normal variable starts again from 10 on every function call. The static variable remembers its previous value and continues from where the previous call ended.

Expected Output

Normal = 15, Static = 15

Normal = 15, Static = 20

Normal = 15, Static = 25

Storage Classes — Exercise 10

Problem

Write a C program that demonstrates the use of auto, register, static and extern storage class concepts in one program.

Program

#include <stdio.h> int globalValue = 100; void demonstrate() { auto int automaticValue = 10; register int registerValue = 20; static int staticValue = 0; // Static value retains its previous value staticValue++; printf("Auto = %d\n", automaticValue); printf("Register = %d\n", registerValue); printf("Static = %d\n", staticValue); } int main() { extern int globalValue; printf("Extern = %d\n\n", globalValue); // Call function multiple times demonstrate(); demonstrate(); return 0; }

Explanation

This program demonstrates the basic behavior of four storage-class concepts. The automatic variable is recreated on each function call, the register variable is a local variable requested for register storage, the static variable retains its value, and the extern declaration refers to a variable defined elsewhere in the program.

Expected Output

Extern = 100

Auto = 10

Register = 20

Static = 1

Auto = 10

Register = 20

Static = 2