Storage classes in C

Storage Classes in C

In C, variables are declared based on the type of data they hold.
During program execution, variables may be stored in:

  • CPU registers, or

  • Primary memory

To specify:

  • where variables are stored

  • how long they exist

  • where they can be accessed

  • their default values

C provides four storage class specifiers:

  1. Automatic

  2. External

  3. Register

  4. Static


What Storage Classes Define

Storage classes specify:

  • Storage location (memory or CPU register)

  • Default initial value

  • Scope (where accessible)

  • Lifetime (how long stored in memory)

To fully define a variable, we must mention:

  • Data type

  • Storage class

A variable name identifies a physical memory location where bits are stored.


Lifetime of a Variable

Definition

Lifetime = duration between memory allocation and deallocation

Types of Lifetime in C

1. Automatic Lifetime

  • Created when execution enters a block/function

  • Destroyed when execution leaves it

  • Stored in function call stack


2. Static Lifetime

  • Created once when declared first time

  • Exists until program termination

  • Stored in data segment


3. Dynamic Lifetime

  • Memory allocated/deallocated manually

  • Uses:

    • malloc()

    • free()

  • Stored in heap


Scope of a Variable

Definition

Scope = region where the variable is accessible

(Scope is a subset of lifetime)

Types of Scope

1. Global Scope

  • Declared outside all functions

  • Accessible throughout program

2. Local Scope

  • Declared inside function/block

  • Accessible only within that block

3. Function Scope

  • Variables passed as formal parameters

  • Accessible within that function


Summary Table: Storage Classes

Storage Class    Storage Area    Default ValueLifetimeScope    Keyword
Automatic    Memory    GarbageTill block executes    Local    auto
Register    CPU Register    GarbageTill block executes    Local    register
Static    Memory    ZeroBetween function calls    Local    static
External    Memory    ZeroEntire program    Global    extern

Declaration Syntax

auto int x; extern int y; register int z; static int s;

1. Automatic Storage Class

  • Default for variables declared inside functions

  • Keyword: auto

  • Stored in primary memory

  • Acts as local variable

  • Not initialized by default (garbage value)

Example

#include <stdio.h> int i=10; void f1() { auto int i=20; printf("The value of i in function f1 is =%d",i); } void f2() { printf("The value of i in function f2 is=%d",i); } int main() { f1(); // prints 20 (local variable) f2(); // prints 10 (global variable) }

2. External Storage Class

  • Used for global variables shared across multiple files

  • Keyword: extern

  • Stored in primary memory

  • Default value = 0

  • Exists throughout program execution

Example

File: prgm1.c

#include <stdio.h> int i; void show(void); int main() { show(); printf("The value of i in prgm1.c=%d",i); }

File: prgm2.c

extern int i; void show() { i=20; printf("The value of i in prgm2.c=%d",i); }

Output

The value of i in prgm2.c=20 The value of i in prgm1.c=20

3. Register Storage Class

  • Stored in CPU register for faster access

  • Keyword: register

  • Usually applied to int and char

  • Default value = garbage

  • Scope limited to function/block

Example

#include <stdio.h> int power(int, register int); int main() { int x=4, k=3, p; p=power(x,k); printf("The value of %d power %d is=%d",x,k,p); } int power(int x, register int k) { register int temp=1; for(;k;k--) temp=temp*x; return temp; }

Output

The value of 4 power 3 is 64

4. Static Storage Class

Makes a variable permanent within a region

Types

  • Static local variable

  • Static global variable

Default value = 0


Static Local Variable

  • Stored permanently in memory

  • Accessible only within function/block

  • Retains previous value between function calls

Example

#include <stdio.h> void show(void) { static int i; printf("The value of static variable i=%d\n",i); i++; } int main() { printf("First call of show function\n"); show(); printf("Second call of show function\n"); show(); printf("Third call of show function\n"); show(); }

Output

First call of show function The value of static variable i=0 Second call of show function The value of static variable i=1 Third call of show function The value of static variable i=2

University Question

Code

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

If called 5 times → Output

Count: 1 Count: 2 Count: 3 Count: 4 Count: 5

Justification

  • static int count = 0; initializes only once

  • Variable retains its value between function calls

  • Each call increments the same stored variable

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays