The <code>auto</code> storage class in C is the default storage class for local variables inside functions. It means the variable is stored in the stack and exists only during the function execution.
Click to reveal answer
beginner
Where are auto variables stored in memory?
auto variables are stored in the stack memory. They are created when the function is called and destroyed when the function ends.
Click to reveal answer
beginner
What is the lifetime of an auto variable?
The lifetime of an auto variable is limited to the block or function in which it is declared. It is created when the block starts and destroyed when the block ends.
Click to reveal answer
beginner
Does explicitly writing auto before a local variable change its behavior?
No, explicitly writing auto before a local variable does not change its behavior because local variables are auto by default.
Click to reveal answer
beginner
Can auto variables be initialized at declaration?
Yes, auto variables can be initialized when declared, just like any local variable.
Click to reveal answer
What is the default storage class for local variables inside a function in C?
Aextern
Bstatic
Cauto
Dregister
✗ Incorrect
Local variables inside functions are auto by default, meaning they have automatic storage duration.
Where are auto variables stored during program execution?
AHeap
BStack
CData segment
DCode segment
✗ Incorrect
auto variables are stored in the stack memory, which is used for function calls and local variables.
What happens to an auto variable when the function ends?
AIt remains in memory
BIt becomes global
CIt is moved to heap
DIt is destroyed
✗ Incorrect
auto variables are destroyed when the function or block ends, freeing their memory.
Does writing auto explicitly before a local variable change its scope?
ANo, it remains local
BYes, it makes it static
CYes, it makes it global
DYes, it makes it external
✗ Incorrect
Explicitly writing auto does not change the variable's scope; it remains local to the block.
Can auto variables be initialized when declared?
AYes, they can be initialized
BOnly if declared extern
COnly if declared static
DNo, they must be assigned later
✗ Incorrect
auto variables can be initialized at the time of declaration like any local variable.
Explain what the auto storage class means in C and how it affects a variable's lifetime and scope.
Think about where local variables live and how long they last.
You got /4 concepts.
Describe the difference between auto and static storage classes in C.
Compare how long variables live and where they are stored.
You got /5 concepts.
Practice
(1/5)
1. What does the auto storage class specify in C?
easy
A. The variable is stored in global memory and accessible everywhere.
B. The variable is stored in static memory and retains value between calls.
C. The variable is local to the function or block and has automatic lifetime.
D. The variable is stored in dynamic memory allocated at runtime.
Solution
Step 1: Understand the meaning of auto in C
The auto keyword makes a variable local to the block or function where it is declared.
Step 2: Recognize the lifetime of auto variables
Such variables exist only while the function or block runs, then they are destroyed.
Final Answer:
The variable is local to the function or block and has automatic lifetime. -> Option C
Quick Check:
auto = local, automatic lifetime [OK]
Hint: Remember: auto means local and temporary [OK]
Common Mistakes:
Confusing auto with static storage
Thinking auto variables are global
Assuming auto variables persist after function ends
2. Which of the following is the correct syntax to declare an auto variable in C?
easy
A. int count auto = 5;
B. int auto count = 5;
C. auto = int count 5;
D. auto int count = 5;
Solution
Step 1: Recall the correct order of storage class and type
In C, the storage class specifier auto comes before the type name.
Step 2: Check each option's syntax
auto int count = 5; uses auto int count = 5; which is correct syntax. Others have incorrect order or invalid syntax.
Final Answer:
auto int count = 5; -> Option D
Quick Check:
Storage class before type = correct syntax [OK]
Hint: Storage class comes before type in declaration [OK]
Common Mistakes:
Placing auto after the type
Using assignment without type
Incorrect order of keywords
3. What will be the output of this C code?
void func() {
auto int x = 10;
x++;
printf("%d ", x);
}
int main() {
func();
func();
return 0;
}
medium
A. 11 12
B. 11 11
C. 10 10
D. 12 12
Solution
Step 1: Understand the behavior of auto variables in function
The variable x is declared as auto int, so it is local and reinitialized each time func() runs.
Step 2: Trace the function calls
Each call sets x = 10, then increments to 11, and prints 11. So output is "11 11 "
Final Answer:
11 11 -> Option B
Quick Check:
auto variable resets each call = 11 11 [OK]
Hint: auto resets each call, so output repeats [OK]
Common Mistakes:
Assuming x keeps incrementing across calls
Confusing auto with static variables
Expecting output to be 11 12
4. Identify the error in this code snippet:
void example() {
auto int count = 5;
static int count = 10;
printf("%d", count);
}
medium
A. Cannot declare two variables with the same name in the same scope.
B. auto and static cannot be used together in the same declaration.
C. auto variables cannot be initialized.
D. printf syntax is incorrect.
Solution
Step 1: Check variable declarations in the same scope
Both count variables 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 A
Quick Check:
Same name in one scope = error [OK]
Hint: No duplicate variable names in one scope [OK]
Common Mistakes:
Thinking auto and static conflict in one declaration
Believing auto variables can't be initialized
Ignoring variable redeclaration rules
5. Consider this code:
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?
hard
A. 2 1 - Inner block has its own auto variable hiding outer one.
B. 1 1 - Outer variable is used both times.
C. 2 2 - Inner variable changes outer variable.
D. Compilation error due to duplicate variable names.
Solution
Step 1: Analyze variable scopes
The inner block declares a new auto int a = 2; which hides the outer a inside the block.
Step 2: Trace the print statements
Inside the block, a is 2, so prints "2 ". Outside, the outer a is still 1, so prints "1".
Final Answer:
2 1 - Inner block has its own auto variable hiding outer one. -> Option A
Quick Check:
Inner auto hides outer auto = 2 1 [OK]
Hint: Inner auto shadows outer variable in nested blocks [OK]
Common Mistakes:
Assuming inner variable changes outer variable
Expecting same variable used both times
Thinking duplicate names cause error in nested blocks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.