0
0
Cprogramming~20 mins

Why storage classes are needed - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Storage Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of variable with static storage class
What is the output of this C program?
C
#include <stdio.h>

void func() {
    static int count = 0;
    count++;
    printf("%d ", count);
}

int main() {
    for(int i = 0; i < 3; i++) {
        func();
    }
    return 0;
}
A3 3 3
B0 1 2
C1 1 1
D1 2 3
Attempts:
2 left
💡 Hint
Think about how static variables keep their value between function calls.
🧠 Conceptual
intermediate
1:30remaining
Purpose of storage classes in C
Why are storage classes needed in C programming?
ATo define the data type of variables
BTo specify the scope, lifetime, and visibility of variables and functions
CTo allocate memory dynamically during runtime
DTo perform arithmetic operations on variables
Attempts:
2 left
💡 Hint
Think about what controls where and how long a variable exists.
🔧 Debug
advanced
2:00remaining
Identify the error related to storage class
What error will this code produce and why?
C
#include <stdio.h>

int main() {
    extern int x;
    printf("%d", x);
    return 0;
}
ALinker error: undefined reference to 'x'
BRuntime error: segmentation fault
CCompilation error: 'extern' cannot be used inside main
DOutput: 0
Attempts:
2 left
💡 Hint
Check if the variable 'x' is defined anywhere.
📝 Syntax
advanced
1:30remaining
Which option correctly declares a register variable?
Which of the following is the correct way to declare a register variable in C?
Aregister count int = 10;
Bint register count = 10;
Cregister int count = 10;
Dint count register = 10;
Attempts:
2 left
💡 Hint
The storage class specifier comes before the type.
Predict Output
expert
2:30remaining
Output of code with auto and static variables
What is the output of this C program?
C
#include <stdio.h>

void test() {
    auto int a = 5;
    static int b = 5;
    a++;
    b++;
    printf("%d %d\n", a, b);
}

int main() {
    test();
    test();
    return 0;
}
A
6 6
6 7
B
6 6
7 7
C
5 6
6 7
D
6 5
6 6
Attempts:
2 left
💡 Hint
Remember how auto and static variables behave inside functions.