Variable Declaration Interview Questions for Embedded C: Quiz and Code Challenges
62 questions: multiple choice, fill-in-the-blank, a garbage-or-zero table, true/false, 6 code challenges with real compiler output and 12 detailed interview answers. Treat it as a model for every variable declaration interview questions round.
62
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 — Declaration, Definition and Initialization
These variable declaration interview questions in multiple-choice form are typical first-round screening items. Click your answer; explanations always appear. Answers like this one score well in variable declaration interview questions.
1. Which statement is a declaration only (no memory allocated)?
extern announces the name and type; the definition is elsewhere.
2. How many times may a global variable be defined in a whole program?
Exactly once. Declarations may repeat.
3. What does an uninitialized local variable contain?
It occupies stack memory that still holds old data; reading it is undefined behaviour.
4. What does an uninitialized global variable contain?
Static storage duration objects are zero-initialized; the start-up code clears .bss.
5. After int a[5] = {1, 2};, a[4] is:
Remaining elements are zero-initialized.
6. Which identifier is invalid in C?
An identifier must not start with a digit.
7. Which variable lives in the .bss section?
Uninitialized (zero-initialized) globals go to .bss; initialized ones go to .data.
8. Where is the initial value of int g = 5; (global) stored in the firmware image?
Flash holds the initial value; the start-up code copies it into the .data section in SRAM.
9. A header file contains int g_flag; and two .c files include it. What happens at link time?
Each file defines its own copy. The header should contain extern int g_flag;.
10. A file contains extern int g_flag; and no file defines it. The linker reports:
Declared but never defined.
11. Which line is an assignment rather than an initialization?
Initialization happens at definition. y = 5; changes an existing variable.
12. In int a = 1, b, c = 3; (inside a function) which variable is uninitialized?
Each declarator needs its own initializer; b has none.
13. Which global initialization is illegal in C?
Global initializers must be constant expressions; another variable is not one.
14. Which compiler flags help find uninitialized variables?
Data-flow analysis needs optimisation enabled for many warnings.
15. Which naming style best shows the unit of a variable?
A unit suffix prevents unit mistakes.
16. Which property of a variable is decided by the linker/compiler, not by you directly?
You choose the name, type and value; the toolchain assigns the address (SRAM location).
Section 2: Fill in the Blanks
Spaces and capitalisation are ignored.
1. extern int x; is a (declaration or definition)
It allocates no memory.
2. int x = 5; at file scope is both a declaration and a
It allocates storage.
3. An uninitialized global variable has the value
Zero-initialized (.bss).
4. The memory section for initialized globals is (with the dot)
.data.
5. The memory section for zero-initialized globals is (with the dot)
.bss.
6. char s[] = "ARM"; occupies bytes
Three characters plus the terminator.
7. After int v[4] = {7}; the sum of all elements is
7 + 0 + 0 + 0.
8. The keyword used to declare a variable defined in another file is
extern.
9. A local variable lives on the (memory region)
Automatic storage uses the stack.
10. int a[] = {1, 2, 3, 4, 5}; has elements
The size is deduced.
11. An identifier cannot start with a
Only letters and underscore may start it.
12. The linker error for a missing definition begins with “undefined ”
undefined reference to.
Section 3: Garbage, Zero or a Defined Value?
Decide what each program reads. This is the most common kind of variable declaration interview question.
Code
Your answer
Result
int main(void) { int n; printf("%d", n); }
GarbageZeroDefined value
int g; int main(void) { printf("%d", g); }
GarbageZeroDefined value
int main(void) { static int n; printf("%d", n); }
GarbageZeroDefined value
int main(void) { int n = 7; printf("%d", n); }
GarbageZeroDefined value
int main(void) { int a[3] = {1}; printf("%d", a[2]); }
GarbageZeroDefined value
int main(void) { int a[3]; printf("%d", a[0]); }
GarbageZeroDefined value
void f(void) { int x; x++; } (value of x before increment)
GarbageZeroDefined value
static uint8_t buf[16]; (file scope), read buf[5]
GarbageZeroDefined value
Section 4: True or False
True/false items are a favourite warm-up for variable declaration interview questions.
1. A declaration allocates memory.
A declaration only introduces the name and type. A definition allocates memory.
2. A variable may be declared many times but defined only once.
The One Definition Rule.
3. Local variables are automatically initialized to zero.
It must be a constant expression; function calls are not evaluated before main().
7. int a, b, c = 0; initializes all three variables to zero.
Only c is initialized.
8. On the STM32F407 the start-up code copies .data from flash to SRAM and clears .bss before main().
This is the job of Reset_Handler.
Section 5: Code Snippet Challenges
These challenges are the code-reading part of variable declaration interview questions. Every answer shown is real compiler or program output.
Challenge 1 — Output?: Partial array initializer
#include <stdio.h>int main(void)
{
int a[5] = { 1, 2 };
int sum = 0;
for (int i = 0; i < 5; i++) sum += a[i];
printf(“%d %zu\n”, sum, sizeof(a));
return0;
}
What are the two numbers printed?
Output:
3 20
Elements 2 to 4 are zero-initialized, so the sum is 1 + 2 = 3, and sizeof(a) is 5 × 4 = 20 bytes.
Challenge 2 — Find the bug: The accumulator that starts from nowhere
#include <stdio.h>int main(void)
{
int total;
int values[3] = { 4, 8, 12 };
for (int i = 0; i < 3; i++)
total += values[i];
printf(“average = %d\n”, total / 3);
return0;
}
What is wrong, and what does GCC say with -Wall -Wextra -O2?
GCC output:
avg.c: In function ‘main’:
avg.c:8:15: warning: ‘total’ is used uninitialized [-Wuninitialized]
8 | total += values[i];
| ~~~~~~^~~~~~~~~~~~
avg.c:5:9: note: ‘total’ was declared here
5 | int total;
| ^~~~~
total is never initialized, so the result is garbage. Fix: int total = 0;
Challenge 3 — Find the bug: The variable defined in a header
/* flags.h */#ifndef FLAGS_H#define FLAGS_Hint g_error_count; /* in the header! */#endif/* uart.c and main.c both #include “flags.h” */
What happens when you link uart.c and main.c?
Linker output:
/usr/bin/ld: file1.o:(.bss+0x0): multiple definition of `g_error_count'; file2.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
Each file defines g_error_count. Fix: extern int g_error_count; in the header and one definition int g_error_count; in uart.c (which includes the header).
Challenge 4 — Output?: Static versus automatic
#include <stdio.h>staticvoid counter(void)
{
staticint n = 0;
int m = 0;
n++; m++;
printf(“n=%d m=%d\n”, n, m);
}
int main(void)
{
counter(); counter(); counter();
return0;
}
What are the three lines of output?
Output:
n=1 m=1
n=2 m=1
n=3 m=1
n is static: initialized once and retained between calls. m is automatic: re-created and set to 0 on every call.
a has exactly the size of the literal plus terminator (3). b was given 8 bytes (the rest zero-filled). strlen counts characters before the zero: 2 for both.
Section 6: Variable Declaration Interview Questions for Embedded C (with Detailed Answers)
These variable declaration interview questions test whether you understand what the compiler, linker and start-up code do with each variable. Click a question for a model answer.
Q1. What is the difference between the declaration and the definition of a variable?Basic+
Declaration
Definition
Example
extern int x;
int x = 0;
Allocates memory?
No
Yes
How often?
Any number of times
Exactly once
Typical place
Header file
One source file
The declaration lets the compiler type-check uses in other files; the definition creates the storage the linker connects them to.
Q2. Why do uninitialized local variables contain garbage?Basic+
Automatic variables are placed on the stack (or in registers) when a function is entered. The stack memory is reused without being cleared, so the new variable shows whatever earlier calls stored there. The C standard calls this an indeterminate value; reading it is undefined behaviour. The cost of initializing is one store, which is why always initialize is the rule.
Q3. Are global variables zero-initialized? Who does it on a microcontroller?Basic+
Yes: objects with static storage duration are zero-initialized by the language. On a bare-metal STM32F407 the language guarantee is implemented by the start-up code: Reset_Handler copies the initial values of .data from flash into SRAM and clears .bss (the zero-initialized region) before calling main(). If you replace or skip the start-up file, you must do this yourself.
Q4. What are the rules for naming variables, and what conventions do embedded teams use?Basic+
Rules: letters, digits and underscore; not starting with a digit; case-sensitive; not a keyword; avoid reserved patterns (__x, _X). Conventions: lower_case with units in the name (timeout_ms), g_ for globals, s_ for file-static, UPPER_CASE for constants and macros, p_/_ptr for pointers.
Q5. How do you share a variable between two source files correctly?Intermediate+
Define it once in a .c file and declare it with extern in a header that every user (and the defining file) includes:
Including the header in the defining file lets the compiler verify the declaration and the definition agree.
Q6. What is the difference between initialization and assignment?Intermediate+
Initialization is giving a value at the moment the object is created (int x = 5;, int a[3] = {1,2,3};). Assignment replaces the value of an existing object (x = 6;). The difference matters for arrays (you can initialize but not assign with =), for const objects (initialize only), and for static variables (initializer runs once).
Q7. Why can GCC fail to warn about an uninitialized variable?Intermediate+
The warning -Wuninitialized relies on data-flow analysis that is only performed when optimisation is enabled. At -O0 many cases are not diagnosed. Even with optimisation, the variable passed by address to another function is not flagged. So: build with -O1 or higher together with -Wall -Wextra, use static analysers (cppcheck, clang-tidy) and run with sanitizers on the host.
Q8. Why is int a, b, c = 0; considered bad style (MISRA, CERT)?Intermediate+
Only c is initialized: a and b hold garbage, and readers often assume otherwise. Writing one declaration per line with its own initializer removes the ambiguity and makes diffs and comments clearer.
Q9. Where do int g = 5;, int h;, const int k = 5; and a local int x; live on the STM32F407?Intermediate+
Variable
Section
Memory
int g = 5;
.data
SRAM (initial value also in flash)
int h;
.bss
SRAM, cleared at reset
const int k = 5; (global)
.rodata
Flash only
local int x;
stack (or a register)
SRAM, not cleared
Lesson 4.3 explores these placements with addresses.
Q10. Old code linked fine with int x; in two files. A newer GCC reports multiple definition. Why?Advanced+
Before GCC 10, -fcommon was the default: an uninitialized file-scope int x; was a tentative definition placed in a “common” block, and the linker merged duplicates silently. Since GCC 10 the default is -fno-common, so every such definition is a real one and duplicates are errors:
GCC 10+ default (real output)
/usr/bin/ld: file1.o:(.bss+0x0): multiple definition of `x’; file2.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
With -fcommon (old behaviour)
(no output: links without errors)
The correct fix is not the flag but proper extern declarations in a header and one definition. Relying on common symbols hides real bugs and is not portable ISO C.
Q11. How can a variable keep its value across a software reset, and why would you want that?Advanced+
Place it in a section that the start-up code does not clear. With GCC and a linker script that provides a .noinit section, write: __attribute__((section(".noinit"))) uint32_t g_boot_reason;. The start-up code clears .bss and copies .data but ignores .noinit, so the value survives a watchdog or soft reset (SRAM is retained while power stays on). Uses: boot counters, crash information, a flag telling the bootloader to stay in update mode. Always protect it with a magic number or checksum, because after a cold power-up it contains random data.
Q12. Can the compiler legitimately remove code that reads an uninitialized variable? Give an example.Advanced+
Yes: the value is indeterminate and the behaviour undefined, so the optimiser is free to assume any value (or none). In int x; if (x == 0 || x != 0) f(); it may call or skip f(); in int x; x = x + 1; it may delete the statement. Such code is not just “random”: it can change meaning between compiler versions and optimisation levels. The exception is reading through unsigned char / uint8_t, which the standard treats more leniently for objects whose address is taken, but code should still not depend on it.
Study Plan: Using Your Score on These Variable Declaration Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 4.2: scope and lifetime.
70–89%
Good
Redo Examples 1, 2, 6 and 7 on the examples page.
Below 70%
Revise
Re-read theory sections 3, 4, 5 and 7, then retake the quiz.
Next lesson: Lesson 4.2 answers the questions that came up here: where can a name be used (scope) and how long does the storage exist (lifetime)?
Lesson 4.1 complete!
Next up: block, file and global scope, and automatic, static and extern lifetime.
2 Comments