C Storage Classes

C Storage Classes

Storage classes in C define the scope, lifetime, and visibility of variables and functions within a program.

They tell the compiler:

βœ” Where the variable is stored
βœ” How long the variable exists (lifetime)
βœ” Who can access it (scope)
βœ” What is its default value


πŸ”Ή Available Storage Classes in C

Storage Class Scope Lifetime Default Value Memory Location
auto Local (block) Function/block duration Garbage value Stack
register Local Function/block duration Garbage value CPU register (if available)
static Local or Global Entire program duration Zero Memory (Static/Data Segment)
extern Global Entire program duration Zero Memory (Static/Data Segment)


1️⃣ auto (Automatic Storage)

  • Default for local variables

  • Exists only inside functions or blocks

  • Stored in stack memory

#include <stdio.h>

int main() {
auto int x = 10; // same as: int x = 10;
printf("%d", x);
return 0;
}

βœ” Usually we don’t use auto keyword explicitly because it’s default.



2️⃣ register

  • Suggests storing variable in CPU register for faster access

  • Cannot take its memory address using &

#include <stdio.h>

int main() {
register int counter;

for(counter = 1; counter <= 5; counter++) {
printf("%d ", counter);
}

return 0;
}

❗ Note: Compiler may ignore this request.



3️⃣ static

A static variable preserves its value between function calls.

βœ” Example: Local static variable

#include <stdio.h>

void test() {
static int count = 0;
count++;
printf("Count: %d\n", count);
}

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

Output:

Count: 1
Count: 2
Count: 3

πŸ”Ή Unlike normal local variables, static does not reset.


βœ” Static Global Variable

A static global variable is accessible only inside the file, not from other .c files.

static int number = 100; // File-level visibility only


4️⃣ extern

extern is used to declare a variable that is defined in another file or location.

Useful in multi-file projects.


πŸ“Œ Example

πŸ“ file1.c

#include <stdio.h>

int num = 10; // Definition

πŸ“ file2.c

#include <stdio.h>

extern int num; // Declaration

int main() {
printf("%d", num);
return 0;
}

βœ” extern does not allocate memory β€” it only references existing storage.



🧠 Summary Table

Keyword Use Case
auto Default local variable
register Faster access storage suggestion
static Persistent variable or file-level global variable
extern Global variable sharing between files

Example Using All Storage Classes

#include <stdio.h>

int global = 5; // global variable

void show() {
static int count = 0; // static local variable
count++;
printf("Static Count = %d\n", count);
}

int main() {

auto int a = 10; // automatic local variable
register int r = 20; // register variable

extern int global; // refers to global variable

printf("Auto: %d\n", a);
printf("Register: %d\n", r);
printf("Extern Global: %d\n", global);

show();
show();
show();

return 0;
}

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *