Challenge - 5 Problems
Extern Storage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of extern variable usage across files
Consider two files:
File1.c:
File2.c:
What will be the output when
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; }
Attempts:
2 left
💡 Hint
Remember that extern declares a variable defined elsewhere.
✗ Incorrect
The variable 'count' is defined in File1.c and declared as extern in File2.c. Linking both files allows printCount() to access the same variable, printing 5.
❓ Predict Output
intermediate2:00remaining
Value of extern variable after modification
Given the following code snippets:
File1.c:
File2.c:
What is the value of
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; }
Attempts:
2 left
💡 Hint
Extern allows access to the same variable across files.
✗ Incorrect
The extern declaration links to the same variable 'value' defined in File1.c. Changing it in File2.c affects the original variable, so the printed value is 20.
❓ Predict Output
advanced2:00remaining
Output of extern variable with multiple definitions
What happens if you have these two files:
File1.c:
File2.c:
And you compile and link them together, then print
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; }
Attempts:
2 left
💡 Hint
Multiple definitions of the same global variable cause conflicts.
✗ Incorrect
Defining 'num' in both files causes a multiple definition error during linking. Extern does not resolve this conflict if both define the variable.
❓ Predict Output
advanced2:00remaining
Behavior of extern with uninitialized variable
Consider these files:
File1.c:
File2.c:
What is the initial value of
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;
Attempts:
2 left
💡 Hint
Global variables without explicit initialization are zero-initialized.
✗ Incorrect
Global variables like 'data' are automatically initialized to zero if not explicitly initialized. Extern links to the same variable.
🧠 Conceptual
expert2:00remaining
Understanding extern and static linkage
Which statement correctly explains the difference between
extern and static storage classes in C?Attempts:
2 left
💡 Hint
Think about variable visibility and linkage across files.
✗ Incorrect
extern declares a variable defined in another file, giving it external linkage. static restricts the variable's visibility to the file it is declared in, giving it internal linkage.