Bird
Raised Fist0
Cprogramming~20 mins

Why storage classes are needed - Challenge Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. Why are storage classes needed in C programming?
easy
A. To allocate memory dynamically
B. To define the data type of variables
C. To perform arithmetic operations
D. To control the lifetime and visibility of variables

Solution

  1. Step 1: Understand the role of storage classes

    Storage classes in C define how long a variable exists and where it can be accessed.
  2. Step 2: Differentiate from other concepts

    Data types define variable type, arithmetic operations perform calculations, and dynamic memory allocation is separate from storage classes.
  3. Final Answer:

    To control the lifetime and visibility of variables -> Option D
  4. Quick Check:

    Storage classes = control lifetime and visibility [OK]
Hint: Storage classes manage variable life and access [OK]
Common Mistakes:
  • Confusing storage classes with data types
  • Thinking storage classes allocate memory dynamically
  • Assuming storage classes perform operations
2. Which of the following is the correct syntax to declare a variable with static storage class in C?
easy
A. count static int;
B. static int count;
C. int static count;
D. int count static;

Solution

  1. Step 1: Recall correct order of storage class declaration

    In C, the storage class specifier comes before the data type.
  2. Step 2: Verify each option's syntax

    static int count; uses 'static int count;' which is correct. Other options place 'static' incorrectly.
  3. Final Answer:

    static int count; -> Option B
  4. Quick Check:

    Storage class before type = correct syntax [OK]
Hint: Storage class keyword always precedes data type [OK]
Common Mistakes:
  • Placing storage class after variable name
  • Mixing order of keywords
  • Using invalid syntax like 'count static int;'
3. What will be the output of the following C code?
void func() {
    static int count = 0;
    count++;
    printf("%d ", count);
}

int main() {
    for(int i = 0; i < 3; i++) {
        func();
    }
    return 0;
}
medium
A. 1 2 3
B. 3 3 3
C. 0 1 2
D. 1 1 1

Solution

  1. Step 1: Understand static variable behavior inside function

    The static variable 'count' retains its value between function calls and initializes only once.
  2. Step 2: Trace the loop and function calls

    Each call to func() increments count: first call prints 1, second 2, third 3.
  3. Final Answer:

    1 2 3 -> Option A
  4. Quick Check:

    Static variable increments across calls = 1 2 3 [OK]
Hint: Static variables keep value between calls [OK]
Common Mistakes:
  • Assuming static variable resets each call
  • Confusing static with automatic variables
  • Ignoring initialization behavior
4. Identify the error in the following C code related to storage class usage:
extern int x = 10;

int main() {
    printf("%d", x);
    return 0;
}
medium
A. Missing 'static' keyword before 'int x'
B. 'x' should be declared inside main function
C. 'extern' variable cannot be initialized
D. No error, code runs fine

Solution

  1. Step 1: Understand 'extern' storage class rules

    An 'extern' variable declaration cannot have an initializer; it only declares the variable.
  2. Step 2: Analyze the code

    Here, 'extern int x = 10;' tries to initialize 'x' which is invalid and causes a compilation error.
  3. Final Answer:

    'extern' variable cannot be initialized -> Option C
  4. Quick Check:

    Extern declarations must not initialize variables [OK]
Hint: Extern declarations cannot initialize variables [OK]
Common Mistakes:
  • Initializing extern variables
  • Assuming extern means static
  • Declaring variables only inside main
5. You want a variable in C that keeps its value between function calls but is only accessible inside that function. Which storage class should you use?
hard
A. static
B. extern
C. auto
D. register

Solution

  1. Step 1: Understand variable lifetime and scope requirements

    The variable must keep its value between calls (persistent lifetime) and be local to the function (limited scope).
  2. Step 2: Match storage classes to requirements

    'static' inside a function keeps the variable value between calls and restricts scope to that function.
  3. Final Answer:

    static -> Option A
  4. Quick Check:

    Static inside function = persistent and local [OK]
Hint: Use static for persistent local variables [OK]
Common Mistakes:
  • Using auto which resets each call
  • Using extern which is global
  • Using register which is for CPU registers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard