Scope and Linkage Interview Questions for Embedded C: Quiz and Code Challenges
57 questions: multiple choice, write-the-declaration blanks, a feature-matching table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for scope and linkage interview questions.
57
Questions
Beginner → Advanced
Level
Live score
Auto-graded
Live Score Tracker
0 Correct
0 Wrong
0 Attempted
Answer the auto-graded questions (MCQ, fill-in, true/false) to see your score.
Section 1: Multiple Choice Questions — Scope, Lifetime and Linkage
These scope and linkage interview questions appear in nearly every embedded C screening round. Click an answer; explanations always appear.
1. What is the initial value of an uninitialised local variable?
Locals are not cleared; reading one is undefined behavior.
2. What is the initial value of an uninitialised global variable?
Globals are placed in .bss, which start-up code clears.
3. What does static mean on a local variable?
The object lives for the whole program; its scope is still the block.
4. What does static mean on a file-scope function?
Other files cannot call it.
5. Which keyword declares a variable defined in another file?
extern int g; reserves no memory.
6. Where is an initialised global such as int a = 5; stored on an STM32?
Start-up code copies .data from Flash to SRAM.
7. Where does an automatic variable usually live?
It disappears when the block ends.
8. Two files both define int count; (no static). The result is:
Both have external linkage; with GCC 10+ the default is -fno-common.
9. What does shadowing mean?
GCC reports it with -Wshadow.
10. What is the lifetime of a block-scope static variable?
Static storage duration.
11. A function uses a static local and is called from an ISR and from main. It is:
Both contexts share the one variable.
12. Which part of a recursive function stops the recursion?
Without a reachable base case the stack overflows.
13. What is the main danger of recursion on a microcontroller?
Each call uses a stack frame.
14. Which coding standard rule forbids recursion?
MISRA C rule 17.2: functions shall not call themselves.
15. A static function in a.c and another static function of the same name in b.c:
Internal linkage keeps them private.
Section 2: Write or Evaluate (Fill in the Blanks)
Type the declaration for the first three items (spacing is flexible) and a short value for the rest.
1. Write the declaration (for a header) of an int variable g_ticks that is defined in another file:
Use extern; no initialiser.
2. Write the definition line of a file-private counter s_n of type int, starting at 0 (use static):
Both are fine; static storage starts at zero.
3. Write the declaration of a flag g_flag (type uint8_t) shared with an ISR, defined elsewhere:
extern volatile.
4. void f(void) { static int n = 0; n++; printf("%d", n); }. After three calls, the last value printed is
The static keeps counting.
5. void g(void) { int n = 0; n++; printf("%d", n); }. After three calls, the last value printed is
n restarts on every call.
6. The section that holds zero-initialised globals is called .
Block Started by Symbol.
7. The STM32F407 SRAM starts at address 0x
SRAM base address.
8. factorial(5) =
5 × 4 × 3 × 2 × 1.
Section 3: Which Feature Fits?
Choose the storage or linkage feature for each need, as asked in scope and linkage interview questions about design.
Need
Your answer
Result
Variable private to one .c file
static at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Variable shared by several files
static at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Function state kept between calls
static at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
State for many instances
static at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Flag set in an ISR
static at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Block-local temporary
static at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Read-only table kept in Flash
static at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Known worst-case stack use
static at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Section 4: True or False
Statements typical of scope and linkage interview questions.
1. A local variable declared static is visible to other functions.
Only its lifetime is extended; the scope is unchanged.
2. An extern declaration allocates memory.
The definition does.
3. A variable may be declared many times but defined once.
The one-definition rule for external linkage.
4. Local variables are initialised to zero.
They are indeterminate.
5. An inner-block variable can hide an outer variable of the same name.
Shadowing.
6. Static variables are initialised every time the function runs.
Only once, before main.
7. Every recursive function needs a base case.
Otherwise it never stops.
8. A recursive function uses constant stack space.
One frame per level (unless a tail call is optimised).
Section 5: Code Snippet Challenges
Trace each program on paper first; the real output follows. These challenges are the code-reading half of scope and linkage interview questions.
Challenge 1 — Output?: A static local
#include <stdio.h>staticint f(void)
{
staticint n = 10;
n += 5;
return n;
}
int main(void)
{
f();
f();
printf(“%d\n”, f());
return0;
}
What is printed?
Output:
25
n starts at 10 once; each call adds 5, so the third call returns 25.
Challenge 2 — Output?: Shadowing
#include <stdio.h>int x = 1;
int main(void)
{
int x = 2;
{
int x = 3;
x++;
}
printf(“%d\n”, x);
return0;
}
What is printed?
Output:
2
The innermost x (3, then 4) is gone after the block; the middle x, 2, is printed. The global 1 is hidden.
Challenge 3 — Output?: Factorial
#include <stdio.h>staticint fact(int n)
{
if (n <= 1) return1;
return n * fact(n – 1);
}
int main(void)
{
printf(“%d %d\n”, fact(1), fact(6));
return0;
}
What is printed?
Output:
1 720
1! is the base case (1); 6! = 720.
Challenge 4 — Output?: How many calls?
#include <stdio.h>staticint calls;
staticint fib(int n)
{
calls++;
return n < 2 ? n : fib(n – 1) + fib(n – 2);
}
int main(void)
{
int r = fib(7);
printf(“%d %d\n”, r, calls);
return0;
}
What is printed (fib(7) and the number of calls)?
Output:
13 41
fib(7) is 13; the naive recursion needs 41 calls, because it recomputes the same values.
Linker result:/usr/bin/ld: file1.o:(.data+0x0): multiple definition of `counter’; file2.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit statusBoth files define counter with external linkage. Make one static, or declare it extern in one file and define it once.
The static b counts 1, 2, 3. The local a is never initialised, so a++ reads an indeterminate value: undefined behavior, even though the program output does not show it.
Section 6: Scope and Linkage Interview Questions for Embedded C (with Detailed Answers)
Scope and linkage interview questions test storage duration, the meaning of static, sharing variables across files, reentrancy and recursion. Click a question for a model answer.
Q1. What is the difference between scope, storage duration and linkage?Basic+
Concept
Question it answers
Decided by
Scope
Where can I use this name?
Place of the declaration
Storage duration
How long does the object live?
static, automatic or allocated
Linkage
Do the same names in different files mean one entity?
static or not, at file scope
The three are independent: a static local has block scope, static duration and no linkage.
Q2. What are the uses of the static keyword in C?Basic+
On a local variable: static storage duration, so it keeps its value between calls. On a file-scope variable or function: internal linkage, so the name is private to the file. The same keyword, two meanings; interviewers often ask for both.
Q3. Where are global, static and local variables stored on an STM32?Basic+
Variable
Memory section
Region
Initialised global/static
.data
SRAM (image in Flash)
Zero global/static
.bss
SRAM
const data
.rodata
Flash
Local (automatic)
Stack
SRAM
malloc block
Heap
SRAM
Q4. Why are globals zero and locals not?Intermediate+
The start-up code runs before main: it copies the .data image from Flash to SRAM and clears .bss to zero, so every static-duration object has a defined value. A local is allocated by moving the stack pointer; nothing clears the stack, so its old contents appear. This is also why leaving out the initialiser on a local is a bug that may not show up in testing.
Q5. How do you share a variable between two source files correctly?Intermediate+
The header carries the extern declaration; exactly one .c file defines the variable and includes the header, so the compiler cross-checks both.
Q6. What happens when two files define the same global?Intermediate+
The linker reports a multiple-definition error. Historically compilers merged uninitialised tentative definitions (-fcommon), hiding the bug and sharing the memory silently; GCC 10 changed the default to -fno-common. Either make private globals static or define each once and declare it with extern elsewhere.
Q7. What is a reentrant function and how do static locals break it?Intermediate+
A reentrant function can be interrupted and called again (from an ISR or another task) before the first call finishes, without corrupting data. It uses only its parameters and automatic locals, or protected shared data. A static local is shared by all callers, so the second invocation overwrites the first one’s state. The fix is to keep the state in a caller-owned structure.
Q8. Explain how a recursive function uses the stack.Intermediate+
Each call pushes a frame holding the return address (LR), the callee-saved registers it uses, locals and spilled arguments. Frames pile up until the base case returns, then they are released in reverse order. Stack need is depth × frame size, which on a part with a few kilobytes of stack is a hard limit.
Q9. Why do safety standards such as MISRA C forbid recursion?Advanced+
Static analysis cannot prove the worst-case stack depth of recursive code, and a stack overflow on a microcontroller has no guard: it silently corrupts variables or triggers a HardFault. A fixed iterative version or an explicit stack with a bounds check has a provable limit and a defined failure behavior.
Q10. How would you convert recursion into iteration?Advanced+
If the call is a tail call, the loop is direct: replace the call by assignments to the parameters and jump to the top. Otherwise keep your own array stack: push the pending work, loop until the stack is empty, and check for overflow before every push. Example 8 of this lesson does this for a tree traversal and returns an error when the fixed stack is full.
Q11. What is the problem with the naive recursive Fibonacci?Advanced+
It recomputes subproblems: the number of calls grows exponentially (41 calls for fib(7), 21891 for fib(20)). The stack depth is only n, but the CPU time explodes. An iterative loop uses two variables and O(n) time; dynamic programming or a lookup table solves the general problem.
Q12. How can a variable declared in a header cause trouble?Advanced+
If the header contains a definition (int g_x = 0;) every file that includes it creates its own definition: link errors, or with static a separate private copy per file, which hides the bug because each file sees a different variable. Headers should contain only declarations (extern), types, macros and static inline functions.
Study Plan: Using Your Score on These Scope and Linkage Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 8.4: function pointers.
70–89%
Good
Redo Examples 2, 3 and 6 of the examples page.
Below 70%
Revise
Re-read theory sections 2, 3 and 6, then retake the quiz.
Habit to build: For every global ask: does another file need this? If not, make it static.
Lesson 8.3 complete!
Next up: function pointers, callbacks and dispatch tables.
2 Comments