The extern storage class tells the compiler that a variable or function is defined in another file or place. It helps share variables or functions across multiple files.
Extern storage class
extern data_type variable_name; extern return_type function_name(parameter_list);
The extern keyword only declares the variable or function; it does not allocate memory.
The actual variable or function must be defined exactly once elsewhere without extern.
count exists somewhere else.extern int count;display defined in another file.extern void display();message.extern char message[];This example shows two parts: one file defines the variable number, and the other file declares it with extern to use it. When linked together, the program prints the value of number.
// File 1: define.c int number = 10; // definition of variable // File 2: use.c #include <stdio.h> extern int number; // declaration of external variable int main() { printf("Number is %d\n", number); return 0; }
Without extern, declaring the same variable in multiple files causes errors.
extern helps the linker find the actual variable or function during program build.
Use extern only for declarations, not definitions.
extern declares variables or functions defined elsewhere.
It allows sharing data across multiple files.
Helps avoid duplicate definitions and linker errors.
Practice
extern storage class in C?Solution
Step 1: Understand the role of
Theexternexternkeyword tells the compiler that the variable or function is defined elsewhere, usually in another file.Step 2: Compare with other storage classes
Other options like local variables or constants do not useextern. It specifically helps share variables/functions across files.Final Answer:
To declare a variable or function defined in another file -> Option AQuick Check:
externdeclares external variables [OK]
- Confusing extern with static
- Thinking extern allocates memory
- Using extern for local variables
count?Solution
Step 1: Recall correct extern declaration syntax
The correct syntax placesexternbefore the type:extern int count;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.Final Answer:
extern int count; -> Option BQuick Check:
extern before type, no initialization [OK]
- Initializing extern variables
- Placing extern after type
- Combining extern with static
// 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?
Solution
Step 1: Understand extern linking
The variablecountis defined infile1.cwith value 5. Theexterndeclaration infile2.crefers to the same variable.Step 2: Predict output
Sincecountis defined and initialized infile1.c, printing it infile2.coutputs 5.Final Answer:
5 -> Option AQuick Check:
extern links to defined variable value [OK]
- Assuming extern creates a new variable
- Expecting zero as default
- Ignoring multi-file linking
extern int value = 10;
int main() {
printf("%d", value);
return 0;
}
Solution
Step 1: Check extern initialization rules
Anexterndeclaration cannot initialize the variable. Initialization must be done in exactly one place without extern.Step 2: Identify error in code
The code tries to initializevaluewith 10 in the extern declaration, which causes a compilation error.Final Answer:
extern variable cannot be initialized -> Option CQuick Check:
extern declarations must not initialize [OK]
- Initializing extern variables
- Confusing extern with normal definitions
- Ignoring linker errors
// 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?
Solution
Step 1: Understand extern and function usage
totalis defined infile1.cand declared extern infile2.c. The functionupdate()modifiestotal.Step 2: Trace program execution
Main callsupdate(), which adds 50 to total (100 + 50 = 150). Then it prints total, so output is 150.Final Answer:
150 -> Option DQuick Check:
Extern shares variable; function updates value [OK]
- Expecting original value without update
- Forgetting to declare extern
- Confusing declaration and definition
Ready to prove it?
Pass the test and this pattern moves unknown
