KEMBAR78
Storage Classes | PDF | Scope (Computer Science) | Variable (Computer Science)
0% found this document useful (0 votes)
22 views4 pages

Storage Classes

Storage classes in C define how variables are stored in memory, their scope, lifetime, and accessibility. There are four types of storage classes: auto for temporary local variables, register for high-speed access variables, static for persistent variables, and extern for sharing global variables across files. Each storage class has specific characteristics regarding memory location and variable persistence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Storage Classes

Storage classes in C define how variables are stored in memory, their scope, lifetime, and accessibility. There are four types of storage classes: auto for temporary local variables, register for high-speed access variables, static for persistent variables, and extern for sharing global variables across files. Each storage class has specific characteristics regarding memory location and variable persistence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What Are Storage Classes in C?

A storage class in C defines how a variable is stored in memory, its scope (visibility), lifetime
(existence duration), and accessibility within a program. It determines whether a variable is
local or global, whether its value persists between function calls, and where it is stored (RAM
or CPU register).

You can think of storage classes like employee roles in a company. Some employees work
temporarily (auto), some retain their tasks daily (static), some work at the front desk for quick
access (register), and some share resources across teams (extern).

Types of Storage Classes in C


There are 4 types of storage classes in C programming:
1. auto for temporary local variables.
2. register for high-speed variables stored in CPU registers.
3. static for persistent local or global variables.
4. extern for sharing global variables across multiple files.

Auto Storage Classes in C


The auto storage class in C is the default storage class for local variables.

 Scope: Block (local to function).


 Lifetime: Created when function is called, destroyed when function exits.
 Memory Location: RAM.

Example of auto storage class in C:

#include <stdio.h>

void exampleFunction() {
auto int num = 10; // 'auto' keyword is optional
printf("Value of num: %d\n", num);
}

int main() {
exampleFunction();
return 0;
}
Output:

Value of num: 10
Key Point: Auto variables are temporary and exist only within their function.

Register Storage Classes in C (Register Variables)


The register storage class in C suggests that the variable should be stored in CPU registers for
faster access.

 Scope: Block (local to function).


 Lifetime: Created when function is called, destroyed when function exits.
 Memory Location: CPU register (if available, otherwise RAM).

Example:
#include <stdio.h>
void fastFunction(void);

void main()
{
fastFunction();
}

void fastFunction()
{
register int counter = 5; // Hint for CPU register storage
printf("Counter: %d\n", counter);
}

Output:

Counter: 5
 Key Point: Register variables improve performance for frequently used variables
(like loop counters).
 Limitation: You cannot use the address-of (&) operator on register variables.

Static Storage Classes in C (Static Variables)


The static storage class in C retains value between function calls.

 Scope: Local (if used inside a function), Global (if declared outside any function).
 Lifetime: Exists throughout program execution.
 Memory Location: RAM.

Example 1: Static Variable inside a Function


#include <stdio.h>

void countFunction() {
static int count = 0; // Retains value between calls
count++;
printf("Count: %d\n", count);
}

int main() {
countFunction();
countFunction();
countFunction();
return 0;
}
Output:

Count: 1
Count: 2
Count: 3
Example 2: Static Global Variable (Restricted Scope)
#include <stdio.h>

static int globalVar = 10; // Visible only in this file

void display() {
printf("GlobalVar: %d\n", globalVar);
}

int main() {
display();
return 0;
}

Extern Storage Classes in C (External Variables)


The extern storage class in C is used to share global variables across multiple files.

 Scope: Global (across files).


 Lifetime: Exists throughout program execution.
 Memory Location: RAM.

Example (Multiple Files - extern.c and main.c)

File 1: extern.c (Variable Definition)

#include <stdio.h>

int globalVar = 100; // Defined here

void displayGlobal()
{
printf("GlobalVar: %d\n", globalVar);
}
File 2: main.c (Using extern to Access the Variable)

#include <stdio.h>

extern int globalVar; // Declaration (variable is in another file)


void displayGlobal();

int main()
{
printf("Accessing extern variable: %d\n", globalVar);
displayGlobal();
return 0;
}
Output (when compiled together):

Accessing extern variable: 100


GlobalVar: 100

C Storage Classes: Comparison


Storage Scope Lifetime Default Memory Usage
Class Initial Value Location
auto Local Created and Garbage RAM Default for local
(Block) destroyed on (undefined) variables
function call

register Local Created and Garbage CPU For fast-access


(Block) destroyed on (undefined) Register (if variables (e.g.,
function call available) loop counters)

static Local Exists Zero (0) RAM Retains value


(Block) or throughout between function
File program calls
(Global) execution

extern Global Exists Zero (0) RAM Accesses global


(across throughout variables across
files) program files
execution

You might also like