0
0
Cprogramming~5 mins

Extern storage class - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the <code>extern</code> storage class in C?
The <code>extern</code> storage class tells the compiler that a variable or function is defined in another file or location. It allows sharing variables or functions across multiple files.
Click to reveal answer
beginner
How does extern differ from static in C?
extern makes a variable or function visible across multiple files, while static limits visibility to the current file only.
Click to reveal answer
intermediate
What happens if you declare a variable with extern but do not define it anywhere?
The linker will give an error because it cannot find the actual definition of the variable. extern only declares, it does not allocate memory.
Click to reveal answer
beginner
Example: How to declare and use an extern variable across two files?
File1.c: <br> int count = 10; <br> File2.c: <br> extern int count; // refers to count in File1.c <br> This allows File2.c to access the variable count defined in File1.c.
Click to reveal answer
beginner
Can extern be used with functions? If yes, how?
Yes. Functions have external linkage by default. You can explicitly declare a function with extern to indicate it is defined elsewhere, allowing calls across files.
Click to reveal answer
What does the extern keyword do in C?
ADeclares a variable defined in another file
BDefines a new variable in the current file
CMakes a variable private to the current file
DAllocates memory for a variable
If you use extern int x; but never define x, what happens?
AProgram compiles and runs fine
BCompiler error
CVariable <code>x</code> is initialized to zero automatically
DLinker error
Which storage class limits a variable's visibility to the current file?
Aextern
Bauto
Cstatic
Dregister
Are functions extern by default in C?
AYes
BNo
COnly if declared with extern
DOnly if declared static
How do you share a variable between two files in C?
ADeclare it as static in both files
BDefine it in one file and declare it as extern in the other
CDefine it as auto in both files
DUse register keyword
Explain how the extern storage class helps in sharing variables across multiple files in C.
Think about how you tell the compiler a variable exists somewhere else.
You got /4 concepts.
    Describe the difference between extern and static storage classes in terms of variable visibility.
    Consider where the variable can be accessed from.
    You got /4 concepts.