0
0
Cprogramming~20 mins

Extern storage class - Practice Problems & Coding Challenges

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