Challenge - 5 Problems
Auto Storage Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of auto variable inside a function
What is the output of this C program?
C
#include <stdio.h> void func() { auto int x = 5; x++; printf("%d ", x); } int main() { func(); func(); return 0; }
Attempts:
2 left
💡 Hint
Remember that 'auto' variables inside functions are reinitialized each call.
✗ Incorrect
The variable 'x' is declared as auto inside func(), so it is created anew each time func() is called. It starts at 5, then increments to 6, and prints 6 each time.
❓ Predict Output
intermediate2:00remaining
Effect of auto keyword on global variables
What error or output does this C code produce?
C
auto int x = 10; int main() { printf("%d", x); return 0; }
Attempts:
2 left
💡 Hint
Check if 'auto' can be used outside functions.
✗ Incorrect
The 'auto' storage class is only valid for local variables inside functions. Using it for global variables causes a compilation error.
🔧 Debug
advanced2:00remaining
Identify the error with auto variable redeclaration
What error does this code produce?
C
int main() { auto int a = 5; auto int a = 10; printf("%d", a); return 0; }
Attempts:
2 left
💡 Hint
Can you declare the same variable twice in the same scope?
✗ Incorrect
Declaring 'a' twice in the same scope causes a compilation error due to redeclaration.
🧠 Conceptual
advanced2:00remaining
Behavior of auto variables in loops
What is the output of this program?
C
#include <stdio.h> int main() { for (int i = 0; i < 3; i++) { auto int x = 0; x += i; printf("%d ", x); } return 0; }
Attempts:
2 left
💡 Hint
Each loop iteration creates a new 'x' starting at 0.
✗ Incorrect
The variable 'x' is auto inside the loop, so it resets to 0 each iteration, then adds i, printing 0, 1, 2.
❓ Predict Output
expert2:00remaining
Auto variable and pointer behavior
What is the output of this C program?
C
#include <stdio.h> int* func() { auto int x = 10; return &x; } int main() { int* p = func(); printf("%d", *p); return 0; }
Attempts:
2 left
💡 Hint
Think about the lifetime of 'x' after func() returns.
✗ Incorrect
The function returns the address of an auto variable 'x' which is destroyed after the function ends. Accessing it causes undefined behavior and likely garbage output.