Storage Class #1: auto
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program to Demonstrate the auto Storage Class and Variable Scope

📑 On this page:
  • What is the auto Storage Class?
  • Key Rules
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • What the auto storage class means — and why it's the default for locals
  • How auto variables live on the stack, created at block entry and destroyed at block exit
  • How an inner auto shadows an outer one with the same name
  • Why each function call gets a fresh copy — no persistence between calls
  • Why trying to access an auto variable outside its block is a compile error

What is the auto Storage Class?

The auto storage class is the default for any variable declared inside a function or block. Writing auto explicitly is legal but redundant — its main use in teaching is to make the concept visible. An auto variable:

  • Lives on the stack
  • Exists only for the duration of its block
  • Starts with garbage (uninitialized) unless you assign a value
  • Is re-created every time its enclosing function is called

💡 Quick Definition: auto means "automatic storage duration" — the compiler allocates the variable automatically when its block begins, and releases it automatically when the block ends. You never call malloc or free for an auto variable.

C program demonstrating the auto storage class and variable scope

Key Rules

Property Value
StorageStack (or CPU register if optimized)
Default valueGarbage (uninitialized)
ScopeInside the enclosing block / function
LifetimeFrom block entry to block exit
LinkageNone
Can be declared global?No — auto at file scope is a compile error

Complete C Program

Program: auto variables, shadowing, and scope

#include <stdio.h>

/* Function 1: demonstrates that each call gets a FRESH auto variable */
void showAuto(void) {
    auto int x = 10;    /* created fresh on every call */
    x = x + 5;
    printf("Inside showAuto(): x = %d\n", x);
}

/* Function 2: demonstrates shadowing of an outer variable by an inner one */
void showShadowing(void) {
    int x = 100;        /* outer auto variable */
    printf("Outer x before block   : %d\n", x);

    {
        int x = 500;    /* inner auto variable shadows the outer one */
        printf("Inner x inside block   : %d\n", x);
    }

    printf("Outer x after block    : %d\n", x);   /* still 100 */
}

/* Function 3: demonstrates that auto variables do not persist */
void showNoPersistence(void) {
    int count = 0;      /* re-initialized each call */
    count = count + 1;
    printf("count in this call     : %d\n", count);
}

int main(void) {
    printf("--- Each call creates a fresh auto variable ---\n");
    showAuto();
    showAuto();

    printf("\n--- Inner auto shadows outer auto ---\n");
    showShadowing();

    printf("\n--- Auto variables do not persist between calls ---\n");
    showNoPersistence();
    showNoPersistence();

    return 0;
}
                                    

Program Output

--- Each call creates a fresh auto variable ---
Inside showAuto(): x = 15
Inside showAuto(): x = 15

--- Inner auto shadows outer auto ---
Outer x before block   : 100
Inner x inside block   : 500
Outer x after block    : 100

--- Auto variables do not persist between calls ---
count in this call     : 1
count in this call     : 1
                                

Step-by-Step Explanation

Let's break down each function and what it demonstrates.

1. Include header and function declarations:

#include <stdio.h>
void showAuto(void);
void showShadowing(void);
void showNoPersistence(void);
                                

stdio.h provides printf. The three function prototypes are just declarations — C requires them when main appears before the function definitions.

2. showAuto — each call gets a fresh copy:

void showAuto(void) {
    auto int x = 10;
    x = x + 5;
    printf("Inside showAuto(): x = %d\n", x);
}
                                

Each call re-runs the initialization x = 10, then adds 5 and prints 15. Because x is auto, its value from the previous call is not remembered — you get 15 every time, not 15 then 20.

3. showShadowing — inner shadows outer:

void showShadowing(void) {
    int x = 100;
    printf("Outer x before block   : %d\n", x);
    {
        int x = 500;
        printf("Inner x inside block   : %d\n", x);
    }
    printf("Outer x after block    : %d\n", x);
}
                                

The inner x = 500 is a different variable — same name, different block, different stack slot. Inside the nested block, the inner one wins. When the block ends, the outer x reappears with its original value (100). This is called shadowing.

4. showNoPersistence — no memory of prior calls:

void showNoPersistence(void) {
    int count = 0;
    count = count + 1;
    printf("count in this call     : %d\n", count);
}
                                

If count were static, it would print 1 then 2. Because it's auto, it resets to 0 every call — hence 1 and 1.

5. Attempting to use x outside its block:

    {
        int x = 500;
        /* ... */
    }
    /* x is GONE here — this line would NOT compile: */
    /* printf("%d", x);  -->  error: 'x' undeclared */
                                

This is the compiler enforcing scope. If you uncomment that line, GCC reports error: 'x' undeclared — proving that auto variables are strictly block-scoped.

🎯 Key Takeaway: auto is the storage class you never have to write — because it's the default. But understanding why it's the default (stack allocation, automatic lifetime, no persistence) is what lets you reason about scope bugs and decide when to switch to static for persistence or register for speed.

Frequently Asked Questions

1. Do I ever need to write the keyword auto?

Almost never. In C, auto is the default for locals — omitting it changes nothing. The keyword is required in a few grammatically obscure cases (e.g., auto int x, y; where the second declarator inherits), but in practice you'll only see it in tutorials. In C++, auto has a completely different meaning — type inference.

2. What is the default value of an auto variable?

Garbage. Unlike static and global variables (which are zero-initialized), auto variables contain whatever was on the stack at that moment. Never read an auto variable before assigning to it.

3. Can I declare an auto variable outside a function?

No. auto int x; at file scope is a compile error in standard C. Global variables have extern linkage — if you want internal file scope, use static.

4. Does the compiler ever store an auto variable in a CPU register?

Yes, routinely. Once the variable's address is never taken and the optimizer sees the benefit, it can be kept entirely in a register — even without the register keyword. Writing register is a hint; the compiler decides.

5. What is the stack depth limit for auto variables?

On typical Linux systems, the stack is limited to around 8 MB. Recursion with large auto arrays can exhaust it — often the cause of "stack overflow" crashes. In such cases, allocate large blocks on the heap with malloc instead.

💡 Tip: Compile with gcc -Wall -Wextra to get warnings when you read an uninitialized auto variable.

📖 Related Tutorials

  • Storage Classes in C — Full Tutorial
  • 2. register Storage Class Performance
  • 3. static Storage Class State
  • 4. extern Storage Class Across Files
  • All Storage Class Assignments

Previous Topic: -->> Storage Classes Assignments (Index)   ||   Next topic: -->> 2. register Storage Class Performance


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.