Quiz- Variable Declaration Interview Questions for Embedded C: Quiz and Code Challenges-Free Embedded C Course in Hyderabad

Need Help or More Information?

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

MODULE 4 · LESSON 4.1 · PRACTICE

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)?

2. How many times may a global variable be defined in a whole program?

3. What does an uninitialized local variable contain?

4. What does an uninitialized global variable contain?

5. After int a[5] = {1, 2};, a[4] is:

6. Which identifier is invalid in C?

7. Which variable lives in the .bss section?

8. Where is the initial value of int g = 5; (global) stored in the firmware image?

9. A header file contains int g_flag; and two .c files include it. What happens at link time?

10. A file contains extern int g_flag; and no file defines it. The linker reports:

11. Which line is an assignment rather than an initialization?

12. In int a = 1, b, c = 3; (inside a function) which variable is uninitialized?

13. Which global initialization is illegal in C?

14. Which compiler flags help find uninitialized variables?

15. Which naming style best shows the unit of a variable?

16. Which property of a variable is decided by the linker/compiler, not by you directly?

Section 2: Fill in the Blanks

Spaces and capitalisation are ignored.

1. extern int x; is a (declaration or definition)

2. int x = 5; at file scope is both a declaration and a

3. An uninitialized global variable has the value

4. The memory section for initialized globals is (with the dot)

5. The memory section for zero-initialized globals is (with the dot)

6. char s[] = "ARM"; occupies bytes

7. After int v[4] = {7}; the sum of all elements is

8. The keyword used to declare a variable defined in another file is

9. A local variable lives on the (memory region)

10. int a[] = {1, 2, 3, 4, 5}; has elements

11. An identifier cannot start with a

12. The linker error for a missing definition begins with “undefined ”

Section 3: Garbage, Zero or a Defined Value?

Decide what each program reads. This is the most common kind of variable declaration interview question.

CodeYour answerResult
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.

2. A variable may be declared many times but defined only once.

3. Local variables are automatically initialized to zero.

4. Global variables are zero by default.

5. Identifiers in C are case-sensitive.

6. A global initializer can call a function.

7. int a, b, c = 0; initializes all three variables to zero.

8. On the STM32F407 the start-up code copies .data from flash to SRAM and clears .bss before main().

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

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

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

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.
Challenge 5 — Complete: Designated array initializer
#include <stdio.h> int main(void) { int arr[4] = { /* ??? */ }; for (int i = 0; i < 4; i++) printf(“%d “, arr[i]); printf(“\n”); return 0; }

Replace /* ??? */ so the program prints 0 0 5 0.

Answer: [2] = 5
0 0 5 0 
A designated initializer sets element 2; all others are zero.
Challenge 6 — Output?: sizeof versus strlen
#include <stdio.h> #include <string.h> int main(void) { char a[] = “hi”; char b[8] = “hi”; printf(“%zu %zu %zu %zu\n”, sizeof(a), sizeof(b), strlen(a), strlen(b)); return 0; }

What are the four numbers?

Output:
3 8 2 2
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+
DeclarationDefinition
Exampleextern int x;int x = 0;
Allocates memory?NoYes
How often?Any number of timesExactly once
Typical placeHeader fileOne 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:

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(“mode = %d\n”, g_mode); return 0; }
Output (verified by compiling and running)
mode = 4

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+
VariableSectionMemory
int g = 5;.dataSRAM (initial value also in flash)
int h;.bssSRAM, cleared at reset
const int k = 5; (global).rodataFlash 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

ScoreMeaningNext step
90–100%ExcellentContinue to Lesson 4.2: scope and lifetime.
70–89%GoodRedo Examples 1, 2, 6 and 7 on the examples page.
Below 70%ReviseRe-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.

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 *