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.
Auto storage class
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.
count with auto storage.auto int count;auto is the same as using auto because it's the default.int number; // same as auto int number;
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.
#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; }
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.
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.
Practice
auto storage class specify in C?Solution
Step 1: Understand the meaning of
Theautoin Cautokeyword makes a variable local to the block or function where it is declared.Step 2: Recognize the lifetime of
Such variables exist only while the function or block runs, then they are destroyed.autovariablesFinal Answer:
The variable is local to the function or block and has automatic lifetime. -> Option CQuick Check:
auto= local, automatic lifetime [OK]
- Confusing auto with static storage
- Thinking auto variables are global
- Assuming auto variables persist after function ends
auto variable in C?Solution
Step 1: Recall the correct order of storage class and type
In C, the storage class specifierautocomes before the type name.Step 2: Check each option's syntax
auto int count = 5; usesauto int count = 5;which is correct syntax. Others have incorrect order or invalid syntax.Final Answer:
auto int count = 5; -> Option DQuick Check:
Storage class before type = correct syntax [OK]
- Placing auto after the type
- Using assignment without type
- Incorrect order of keywords
void func() {
auto int x = 10;
x++;
printf("%d ", x);
}
int main() {
func();
func();
return 0;
}Solution
Step 1: Understand the behavior of
The variableautovariables in functionxis declared asauto int, so it is local and reinitialized each timefunc()runs.Step 2: Trace the function calls
Each call setsx = 10, then increments to 11, and prints 11. So output is "11 11 "Final Answer:
11 11 -> Option BQuick Check:
auto variable resets each call = 11 11 [OK]
- Assuming x keeps incrementing across calls
- Confusing auto with static variables
- Expecting output to be 11 12
void example() {
auto int count = 5;
static int count = 10;
printf("%d", count);
}Solution
Step 1: Check variable declarations in the same scope
Bothcountvariables are declared in the same function scope, which is not allowed.Step 2: Understand storage class rules
Declaring two variables with the same name in the same scope causes a redeclaration error regardless of storage class.Final Answer:
Cannot declare two variables with the same name in the same scope. -> Option AQuick Check:
Same name in one scope = error [OK]
- Thinking auto and static conflict in one declaration
- Believing auto variables can't be initialized
- Ignoring variable redeclaration rules
void test() {
auto int a = 1;
{
auto int a = 2;
printf("%d ", a);
}
printf("%d", a);
}
int main() {
test();
return 0;
}What is the output and why?
Solution
Step 1: Analyze variable scopes
The inner block declares a newauto int a = 2;which hides the outerainside the block.Step 2: Trace the print statements
Inside the block,ais 2, so prints "2 ". Outside, the outerais still 1, so prints "1".Final Answer:
2 1 - Inner block has its own auto variable hiding outer one. -> Option AQuick Check:
Inner auto hides outer auto = 2 1 [OK]
- Assuming inner variable changes outer variable
- Expecting same variable used both times
- Thinking duplicate names cause error in nested blocks
Ready to prove it?
Pass the test and this pattern moves unknown
