0
0
Cprogramming~10 mins

Extern storage class - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extern storage class
Declare variable with extern
Variable defined elsewhere
Linker connects declaration to definition
Variable can be used in this file
Program compiles and runs
Extern tells the compiler a variable is defined in another file, so it links them together for use.
Execution Sample
C
/* file1.c */
int count = 10;

/* file2.c */
#include <stdio.h>
extern int count;
void printCount() {
    printf("Count: %d\n", count);
}
Shows extern declaration in one file accessing a variable defined in another file.
Execution Table
StepActionEvaluationResult
1Compile file1.cint count = 10;Variable 'count' defined with value 10
2Compile file2.cextern int count;Declaration of 'count' without definition
3Link filesLink 'count' in file2.c to definition in file1.cBoth files share same 'count' variable
4Run printCount()printf("Count: %d\n", count);Output: Count: 10
5Program endsNo errorsExecution successful
💡 Program ends after printing count; extern links declaration to definition across files
Variable Tracker
Variablefile1.c (definition)file2.c (extern declaration)After linkingFinal value
count10undefined (declared)linked to 1010
Key Moments - 3 Insights
Why does 'extern int count;' not allocate memory?
Because 'extern' only declares the variable exists elsewhere; memory is allocated where it is defined (see execution_table step 2 and 3).
What happens if the variable is not defined in any file?
Linker will give an error because extern expects a definition somewhere (see execution_table step 3).
Can you change the value of an extern variable?
Yes, since it refers to the actual variable defined elsewhere, changes affect the same memory (implied by step 4 output).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after linking?
Aundefined
B10
C0
DLinker error
💡 Hint
Check execution_table row 3 and variable_tracker final value for 'count'
At which step does the linker connect the extern declaration to the variable definition?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table step describing linking action
If 'count' was not defined in file1.c, what would happen during linking?
ALinker error occurs
BProgram runs normally
CVariable 'count' is set to 0 automatically
DCompiler error at file2.c
💡 Hint
Refer to key_moments about missing definition and execution_table step 3
Concept Snapshot
extern storage class in C:
- Declares variable exists elsewhere
- Does NOT allocate memory
- Used to share variables across files
- Linker connects extern to definition
- Allows access to same variable in multiple files
Full Transcript
The extern storage class in C tells the compiler that a variable is defined in another file. When you write 'extern int count;', you declare the variable without creating it. The actual variable must be defined elsewhere, like 'int count = 10;' in another file. During linking, the compiler connects the extern declaration to the real variable. This way, you can use the same variable across multiple files. If the variable is not defined anywhere, the linker will give an error. Using extern helps organize code by sharing variables without duplication.