Interview Questions
C Variables · Study material
C Variables Interview Questions and Answers
1. What is the difference between scope and lifetime?
Scope is the part of the program where a name can be used. Lifetime (storage duration) is the time during which the storage exists. A static local has block scope but lives for the whole program.
2. What are the default values of local and global variables?
Globals and static variables are zero-initialised. Local (automatic) variables are indeterminate until assigned.
3. What is the difference between declaration and definition?
A declaration introduces a name and type (extern int x;); a definition also creates the storage (int x; or int x = 5;). There may be many declarations but only one definition.
4. What does static mean for a local variable?
The variable is created once, initialised once and keeps its value between calls; its scope is still the block.
5. What does static mean for a global variable?
Internal linkage: the variable is visible only in the file that defines it.
6. What is the purpose of extern?
To declare a variable or function that is defined elsewhere, usually in another source file.
7. What is the register storage class?
A hint to keep a variable in a CPU register. Its address cannot be taken. Modern compilers allocate registers better than any hint.
8. What is variable shadowing?
A variable in an inner scope with the same name as one in an outer scope; the inner one hides the outer within its block.
9. What is linkage?
Whether a name refers to the same entity across files: external (shared), internal (one file) or none (locals).
10. What is thread_local?
A storage class (C23 keyword, _Thread_local in C11) giving each thread its own instance of the variable.
11. What is a tentative definition?
A file-scope declaration without an initializer and without extern, such as int x;. If no other definition appears in the file, it becomes a definition initialised to 0.
12. What does auto mean in C23?
Used with an initializer, it infers the variable's type from that initializer, e.g. auto n = 10; declares an int.