0
0
Cprogramming~5 mins

Auto storage class

Choose your learning style9 modes available
Introduction

The auto storage class in C tells the compiler to create a variable that only exists inside a function or block. It helps keep variables temporary and local.

When you want a variable to exist only inside a function or block.
When you want to save memory by not keeping variables after the function ends.
When you want to avoid variable conflicts by keeping variables local.
When you want to declare simple local variables without special storage needs.
Syntax
C
auto type variable_name;

By default, variables declared inside functions are auto, so you usually don't need to write auto explicitly.

The auto keyword only applies to local variables inside functions or blocks.

Examples
This declares a local integer variable named count with auto storage.
C
auto int count;
Declaring a local variable without auto is the same as using auto because it's the default.
C
int number;  // same as auto int number;
Sample Program

This program shows a local variable x declared with auto inside the function demo. It prints the value inside the function. The variable x does not exist outside demo.

C
#include <stdio.h>

void demo() {
    auto int x = 5;  // local variable with auto storage
    printf("x inside demo: %d\n", x);
}

int main() {
    demo();
    // x is not accessible here
    // printf("x in main: %d", x); // would cause error
    return 0;
}
OutputSuccess
Important Notes

Using auto explicitly is rare because local variables are auto by default.

auto variables are created when the block starts and destroyed when it ends.

Summary

auto makes a variable local to a function or block.

Local variables are auto by default, so you usually don't write it.

auto variables only exist while the function or block runs.