0
0
Cprogramming~20 mins

Auto storage class - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Auto Storage Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A6 6
B6 7
C5 6
D5 5
Attempts:
2 left
💡 Hint
Remember that 'auto' variables inside functions are reinitialized each call.
Predict Output
intermediate
2: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;
}
A0
B10
CCompilation error: 'auto' not allowed for global variables
DUndefined behavior
Attempts:
2 left
💡 Hint
Check if 'auto' can be used outside functions.
🔧 Debug
advanced
2: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;
}
ACompilation error: redeclaration of 'a'
B5
C10
DUndefined behavior
Attempts:
2 left
💡 Hint
Can you declare the same variable twice in the same scope?
🧠 Conceptual
advanced
2: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;
}
A0 1 3
B0 1 2
C0 0 0
D1 2 3
Attempts:
2 left
💡 Hint
Each loop iteration creates a new 'x' starting at 0.
Predict Output
expert
2: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;
}
A10
B0
CCompilation error
DGarbage value (undefined behavior)
Attempts:
2 left
💡 Hint
Think about the lifetime of 'x' after func() returns.