Quiz- Scope and Linkage Interview Questions for Embedded C: Quiz and Code Challenges-Free Embedded C Programming Course

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

MODULE 8 · LESSON 8.3 · PRACTICE

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?

2. What is the initial value of an uninitialised global variable?

3. What does static mean on a local variable?

4. What does static mean on a file-scope function?

5. Which keyword declares a variable defined in another file?

6. Where is an initialised global such as int a = 5; stored on an STM32?

7. Where does an automatic variable usually live?

8. Two files both define int count; (no static). The result is:

9. What does shadowing mean?

10. What is the lifetime of a block-scope static variable?

11. A function uses a static local and is called from an ISR and from main. It is:

12. Which part of a recursive function stops the recursion?

13. What is the main danger of recursion on a microcontroller?

14. Which coding standard rule forbids recursion?

15. A static function in a.c and another static function of the same name in b.c:

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:

2. Write the definition line of a file-private counter s_n of type int, starting at 0 (use static):

3. Write the declaration of a flag g_flag (type uint8_t) shared with an ISR, defined elsewhere:

4. void f(void) { static int n = 0; n++; printf("%d", n); }. After three calls, the last value printed is

5. void g(void) { int n = 0; n++; printf("%d", n); }. After three calls, the last value printed is

6. The section that holds zero-initialised globals is called .

7. The STM32F407 SRAM starts at address 0x

8. factorial(5) =

Section 3: Which Feature Fits?

Choose the storage or linkage feature for each need, as asked in scope and linkage interview questions about design.

NeedYour answerResult
Variable private to one .c filestatic at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Variable shared by several filesstatic at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Function state kept between callsstatic at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
State for many instancesstatic at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Flag set in an ISRstatic at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Block-local temporarystatic at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Read-only table kept in Flashstatic at file scopeextern in a header, one definitionstatic localstruct passed by pointerextern volatileautomatic localstatic const arrayexplicit stack or loop
Known worst-case stack usestatic 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.

2. An extern declaration allocates memory.

3. A variable may be declared many times but defined once.

4. Local variables are initialised to zero.

5. An inner-block variable can hide an outer variable of the same name.

6. Static variables are initialised every time the function runs.

7. Every recursive function needs a base case.

8. A recursive function uses constant stack space.

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> static int f(void) { static int n = 10; n += 5; return n; } int main(void) { f(); f(); printf(“%d\n”, f()); return 0; }

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); return 0; }

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> static int fact(int n) { if (n <= 1) return 1; return n * fact(n – 1); } int main(void) { printf(“%d %d\n”, fact(1), fact(6)); return 0; }

What is printed?

Output:
1 720
1! is the base case (1); 6! = 720.
Challenge 4 — Output?: How many calls?
#include <stdio.h> static int calls; static int 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); return 0; }

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.
Challenge 5 — Find the bug: Two definitions
/* a.c */ int counter = 1; /* b.c */ int counter = 2; int main(void) { return counter; }

What does the linker say?

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.
Challenge 6 — Output?: Automatic versus static
#include <stdio.h> static void f(void) { int a; static int b; a++; b++; printf(“%d\n”, b); } int main(void) { f(); f(); f(); return 0; }

What is printed? Is the statement a++ safe?

Output:
1
2
3
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+
ConceptQuestion it answersDecided by
ScopeWhere can I use this name?Place of the declaration
Storage durationHow long does the object live?static, automatic or allocated
LinkageDo 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+
VariableMemory sectionRegion
Initialised global/static.dataSRAM (image in Flash)
Zero global/static.bssSRAM
const data.rodataFlash
Local (automatic)StackSRAM
malloc blockHeapSRAM
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+
shared.h
#ifndef SHARED_H #define SHARED_H extern int g_mode; #endif
shared.c
#include “shared.h” int g_mode = 3;
main.c
#include <stdio.h> #include “shared.h” int main(void) { g_mode++; printf(“%d\n”, g_mode); return 0; }
Output (verified by compiling and running)
4

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

ScoreMeaningNext step
90–100%ExcellentContinue to Lesson 8.4: function pointers.
70–89%GoodRedo Examples 2, 3 and 6 of the examples page.
Below 70%ReviseRe-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.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *