Bird
Raised Fist0
Cprogramming~20 mins

Extern storage class - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Extern Storage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of extern variable usage across files
Consider two files:

File1.c:
int count = 5;

File2.c:
extern int count;
void printCount() { printf("%d", count); }


What will be the output when printCount() is called after linking both files?
C
/* File1.c */
int count = 5;

/* File2.c */
#include <stdio.h>
extern int count;
void printCount() { printf("%d", count); }

int main() {
    printCount();
    return 0;
}
A0
BRuntime error due to uninitialized variable
CCompilation error due to missing definition
D5
Attempts:
2 left
💡 Hint
Remember that extern declares a variable defined elsewhere.
❓ Predict Output
intermediate
2:00remaining
Value of extern variable after modification
Given the following code snippets:

File1.c:
int value = 10;

File2.c:
extern int value;
void changeValue() { value = 20; }


What is the value of value after calling changeValue()?
C
/* File1.c */
int value = 10;

/* File2.c */
#include <stdio.h>
extern int value;
void changeValue() { value = 20; }

int main() {
    changeValue();
    printf("%d", value);
    return 0;
}
A10
BUndefined behavior
C20
DCompilation error
Attempts:
2 left
💡 Hint
Extern allows access to the same variable across files.
❓ Predict Output
advanced
2:00remaining
Output of extern variable with multiple definitions
What happens if you have these two files:

File1.c:
int num = 5;

File2.c:
int num = 10;
extern int num;


And you compile and link them together, then print num?
C
/* File1.c */
int num = 5;

/* File2.c */
#include <stdio.h>
int num = 10;
extern int num;

int main() {
    printf("%d", num);
    return 0;
}
ALinker error due to multiple definitions
B5
C10
DRuntime error
Attempts:
2 left
💡 Hint
Multiple definitions of the same global variable cause conflicts.
❓ Predict Output
advanced
2:00remaining
Behavior of extern with uninitialized variable
Consider these files:

File1.c:
extern int data;

File2.c:
int data;


What is the initial value of data when accessed in File1.c?
C
/* File1.c */
#include <stdio.h>
extern int data;

int main() {
    printf("%d", data);
    return 0;
}

/* File2.c */
int data;
ARuntime error
B0
CGarbage value
DCompilation error
Attempts:
2 left
💡 Hint
Global variables without explicit initialization are zero-initialized.
🧠 Conceptual
expert
2:00remaining
Understanding extern and static linkage
Which statement correctly explains the difference between extern and static storage classes in C?
A<code>extern</code> declares a variable defined elsewhere with external linkage; <code>static</code> limits variable visibility to the current file.
B<code>extern</code> creates a new variable local to the function; <code>static</code> shares the variable across multiple files.
C<code>extern</code> initializes a variable to zero; <code>static</code> requires explicit initialization.
D<code>extern</code> and <code>static</code> both make variables visible only inside the function they are declared.
Attempts:
2 left
💡 Hint
Think about variable visibility and linkage across files.

Practice

(1/5)
1. What is the main purpose of the extern storage class in C?
easy
A. To declare a variable or function defined in another file
B. To allocate memory for a variable inside a function
C. To create a constant variable
D. To define a variable local to a block

Solution

  1. Step 1: Understand the role of extern

    The extern keyword tells the compiler that the variable or function is defined elsewhere, usually in another file.
  2. Step 2: Compare with other storage classes

    Other options like local variables or constants do not use extern. It specifically helps share variables/functions across files.
  3. Final Answer:

    To declare a variable or function defined in another file -> Option A
  4. Quick Check:

    extern declares external variables [OK]
Hint: Remember: extern means 'defined outside this file' [OK]
Common Mistakes:
  • Confusing extern with static
  • Thinking extern allocates memory
  • Using extern for local variables
2. Which of the following is the correct syntax to declare an external integer variable named count?
easy
A. int extern count;
B. extern int count;
C. extern int count = 0;
D. static extern int count;

Solution

  1. Step 1: Recall correct extern declaration syntax

    The correct syntax places extern before the type: extern int count;
  2. Step 2: Check invalid options

    int extern count; reverses order, which is invalid. extern int count = 0; initializes the extern variable, which is not allowed. static extern int count; combines static and extern, which conflicts.
  3. Final Answer:

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

    extern before type, no initialization [OK]
Hint: extern must come before type and no initialization [OK]
Common Mistakes:
  • Initializing extern variables
  • Placing extern after type
  • Combining extern with static
3. Consider two files:
// file1.c
int count = 5;

// file2.c
extern int count;
printf("%d", count);

What will be the output when these files are compiled and run together?
medium
A. 5
B. 0
C. Compilation error
D. Undefined behavior

Solution

  1. Step 1: Understand extern linking

    The variable count is defined in file1.c with value 5. The extern declaration in file2.c refers to the same variable.
  2. Step 2: Predict output

    Since count is defined and initialized in file1.c, printing it in file2.c outputs 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    extern links to defined variable value [OK]
Hint: Extern links to defined variable, prints its value [OK]
Common Mistakes:
  • Assuming extern creates a new variable
  • Expecting zero as default
  • Ignoring multi-file linking
4. What is wrong with the following code snippet?
extern int value = 10;
int main() {
printf("%d", value);
return 0;
}
medium
A. No variable named value declared
B. Missing semicolon after extern declaration
C. extern variable cannot be initialized
D. printf syntax is incorrect

Solution

  1. Step 1: Check extern initialization rules

    An extern declaration cannot initialize the variable. Initialization must be done in exactly one place without extern.
  2. Step 2: Identify error in code

    The code tries to initialize value with 10 in the extern declaration, which causes a compilation error.
  3. Final Answer:

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

    extern declarations must not initialize [OK]
Hint: Never initialize extern variables where declared [OK]
Common Mistakes:
  • Initializing extern variables
  • Confusing extern with normal definitions
  • Ignoring linker errors
5. You have two files:
// file1.c
int total = 100;
void update() { total += 50; }

// file2.c
extern int total;
void update();
int main() {
update();
printf("%d", total);
return 0;
}

What will be the output when compiled and run together?
hard
A. Compilation error due to missing extern
B. 100
C. Undefined behavior due to multiple definitions
D. 150

Solution

  1. Step 1: Understand extern and function usage

    total is defined in file1.c and declared extern in file2.c. The function update() modifies total.
  2. Step 2: Trace program execution

    Main calls update(), which adds 50 to total (100 + 50 = 150). Then it prints total, so output is 150.
  3. Final Answer:

    150 -> Option D
  4. Quick Check:

    Extern shares variable; function updates value [OK]
Hint: Extern shares variable; function changes value before print [OK]
Common Mistakes:
  • Expecting original value without update
  • Forgetting to declare extern
  • Confusing declaration and definition
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