Expert · Embedded C, system calls and careers · topic 59 of 60 · 30 min
C interview questions
Explanation
Crisp answers
| Question | Short answer |
|---|---|
| Array vs pointer? | An array is storage for N elements; its name decays to a pointer to the first element in most expressions. sizeof and assignment differ. |
| malloc vs calloc? | malloc(n) gives uninitialised memory; calloc(count, size) zeroes it and checks the multiplication. |
| Meanings of static? | Inside a function: keeps its value between calls. At file level: visible only in that file (internal linkage). On a function: private to its file. |
| What is volatile? | Tells the compiler every access matters (hardware registers, variables changed by ISRs); it does not make code thread-safe. |
| const int *p vs int *const p? | Pointer to const int (cannot change *p) vs const pointer to int (cannot change p). |
| Why is sizeof(struct) bigger than the sum of members? | Padding for alignment; reorder members largest first to reduce it. |
| Little vs big endian? | Little-endian stores the least significant byte first (x86, most ARM); big-endian stores the most significant first (network byte order). |
| Macro vs inline function? | Macros are text substitution without type checks and may evaluate arguments twice; static inline functions are typed and evaluate once. |
| What causes a segmentation fault? | Accessing memory you may not: NULL/wild/dangling pointers, out-of-bounds indexes, writing to string literals, stack overflow. |
| Call by value vs by reference? | C always passes by value; passing a pointer simulates by-reference. |
| What is a dangling pointer? | A pointer to memory that was freed or went out of scope. |
| Difference between ++i and i++? | Both add 1; ++i yields the new value, i++ yields the old value. |
Checking endianness
#include <stdint.h>
int is_little_endian(void) {
uint32_t x = 1;
return *(unsigned char *) &x == 1; /* the first byte in memory holds the 1 on little-endian */
}
/* memory of uint32_t x = 0x01020304:
little-endian: 04 03 02 01 (low byte first)
big-endian: 01 02 03 04 (high byte first) */"What does this print?" favourites
char s[] = "hello"; printf("%zu %zu\n", sizeof s, strlen(s)); /* 6 5 */
int a[] = {1, 2, 3}; int *p = a; printf("%d\n", *(p + 2)); /* 3 */
printf("%d\n", 7 / 2 * 2); /* 6 */
unsigned u = -1; printf("%u\n", u); /* 4294967295 (with 32-bit unsigned) */
int x = 5; printf("%d\n", x++ + 1); /* 6, then x is 6 */
#define SQ(n) n * n
printf("%d\n", SQ(2 + 1)); /* 2 + 1 * 2 + 1 = 5 */
char *q = "abc"; printf("%c\n", *q + 1); /* 'a' + 1 = 'b' */Swap without a temporary (and why not to)
a ^= b; b ^= a; a ^= b; /* swaps two DIFFERENT variables using XOR */
/* but if a and b are the same variable (via pointers), it zeroes it;
it is also no faster on modern CPUs. A temporary variable is clearer: say so in interviews. */Coding questions to practise
- Reverse a string in place; check a palindrome; count words.
- Implement strlen, strcpy (with size), strcmp and atoi yourself.
- Reverse a linked list; detect a cycle (fast and slow pointers); find the middle node.
- Set/clear/toggle a bit; count set bits; check power of two; swap nibbles.
- Write a circular buffer; a stack with an array; a simple memory pool.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
C interview questions · 1/6
In this lesson you will practise the C questions interviewers ask most.
Prefer a video by a teacher? Find free videos on “c programming interview questions embedded static volatile pointers” on YouTube ↗
In real life: An embedded interview round
In a typical first technical round for an embedded role at an automotive supplier, candidates are asked: the meanings of static and volatile, struct padding with an example, how to set bit 5 of a register, little versus big endian with a check function, and to reverse a linked list on paper. Candidates who answer briefly and write correct small code move ahead.
/* "Set bit 5, clear bit 2, toggle bit 7 of an 8-bit register, all in one go" */
volatile uint8_t *reg = (volatile uint8_t *) REG_ADDR; /* REG_ADDR from the vendor header */
*reg = (uint8_t)(((*reg | (1u << 5)) & ~(1u << 2)) ^ (1u << 7));Practice (0/3 done)
What does this print on a typical system? char s[10] = "hi"; printf("%zu %zu\n", sizeof s, strlen(s));
Classic interview algorithm, in JavaScript so it runs here: write hasCycle(head) for a linked list of { value, next } nodes using Floyd's fast and slow pointers (no Set, no marking nodes), and middle(head) returning the middle node's value (for an even count, the second of the two middle nodes).
Answer these ten questions ALOUD, each in under one minute with a tiny C example on paper, and record yourself: array vs pointer; malloc vs calloc; three meanings of static; volatile; const pointer combinations; struct padding; endianness check; macro vs inline; causes of segmentation faults; why i = i++ + ++i is undefined.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Projects and careers with CEmbedded C, system calls and careers · same module
- Embedded patterns: interrupts, timing and constraintsEmbedded C, system calls and careers · same module
- Java interview questionsJava Programming (Core to Spring Boot) · Design patterns, JVM internals and interviews
- C++ interview questions and how to answer themC++ Programming (modern C++ and DSA) · Competitive programming, interviews and careers
- Pointers and functionsPointers
- Go language interview questionsGo Programming (backend and cloud) · Careers and interviews
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
On a little-endian machine, which byte of uint32_t x = 0x11223344 is stored first in memory?
Answer: 0x44
The least significant byte comes first.
What is the best answer to "what does printf("%d %d", i++, i++) print"?
Answer: It is undefined behaviour: i is modified twice without sequencing
Explain and rewrite it.
unsigned int u = -1; on a system with 32-bit unsigned int, u is…
Answer: 4294967295
Conversion to unsigned wraps modulo 2^32.
Which is TRUE about the XOR swap trick?
Answer: It fails (zeroes the value) if both operands are the same object, and is no faster on modern CPUs
Use a temporary.
System.out.println(10 / 4 + " " + 10 / 4.0) prints…Related
What is the difference between malloc and calloc?Related
Answer: calloc zeroes the memory and takes count and size
malloc leaves memory uninitialised.
What is a dangling pointer?Related
Answer: A pointer to memory that was freed or went out of scope
Using it is undefined behaviour.
Which is NOT undefined behaviour?Related
Answer: Unsigned int overflow (it wraps around)
Unsigned arithmetic is defined to wrap modulo 2^bits.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 1 more topic in “Embedded C, system calls and careers” before its exam: Projects and careers with C.
- Module 12 exam: Embedded C, system calls and careers · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- 60 topics left in the course before the final exam.
← Previous: C on Linux: files and processes with system calls