×
>
<

Aptitude

C Storage Class | CrackEase

Storage Class in C

C storage class illustration

Storage classes describe how variables and functions are stored, how long they live, and where they are visible in a C program.

Key properties controlled by storage classes:

  • Scope — where the name is visible (file, block, function).
  • Lifetime — how long the storage exists (runtime or program lifetime).
  • Storage location — approximate area where data is kept (stack, static area, registers, heap).
  • Initial value — whether the object is initialized automatically.
  • Linkage / visibility — whether the same name refers to the same object across translation units.
Storage Class Types :

Types of Storage Classes

In C there are four commonly used storage classes:

  1. auto (automatic)
    • Default for local variables declared inside functions or blocks.
    • Storage is allocated when the block is entered and freed when the block exits.
    • Typical use: local temporaries. The auto keyword is rarely written explicitly.
    • Example:
    • #include <stdio.h>
      
      void display(void) {
          auto int x = 10;     /* same as: int x = 10; */
          printf("auto x = %d\n", x);
      }
      
      int main(void) {
          display();
          return 0;
      }
      
  2. extern (external)
    • Declares a variable or function defined in another translation unit (file) or later in the same file.
    • Used for sharing global variables across files (gives external linkage).
    • Example:
    • /* file1.c */
      #include <stdio.h>
      extern int x;   /* declaration only */
      
      int main(void) {
          printf("extern x = %d\n", x);
          return 0;
      }
      
      /* file2.c */
      int x = 20;     /* definition */
      
    • Note: when using extern across files you must compile and link both translation units together.
  3. register
    • Suggests that the variable be kept in a CPU register for faster access (compiler may ignore this suggestion).
    • Registers cannot have their address taken with the unary & operator (historical; modern compilers may still allow it depending on optimizations).
    • Example:
    • #include <stdio.h>
      
      int main(void) {
          register int i;
          for (i = 0; i < 3; ++i) {
              printf("register i = %d\n", i);
          }
          return 0;
      }
      
  4. static
    • Preserves the value of a variable between function calls (if declared inside a function).
    • If used at file scope (outside functions), it gives the identifier internal linkage (visible only inside that translation unit).
    • Static objects are allocated in static storage and exist for the entire program run.
    • Example (function-local static):
    • #include <stdio.h>
      
      void counter(void) {
          static int count = 0;  /* initialized once */
          printf("count = %d\n", count);
          count++;
      }
      
      int main(void) {
          counter();   /* prints 0 */
          counter();   /* prints 1 */
          return 0;
      }
      
    • Example (file-scope static):
    • /* file.c */
      static int hidden = 42;   /* visible only inside this file */
      

These storage classes determine scope, lifetime and linkage — knowing them helps you write correct and efficient C programs.

Footer Content | CrackEase