Skip to content

Expert · Embedded C, system calls and careers · topic 59 of 60 · 30 min

C interview questions

Explanation

C interviews (embedded, systems, product and service companies) return to the same topics: pointers versus arrays, malloc versus calloc, the meanings of static, volatile and const, struct padding, endianness, macros versus inline functions, dangling pointers and segmentation faults, call by value, and many "what does this print?" snippets. Answer briefly, with a small example, and mention undefined behaviour where it applies.

Crisp answers

QuestionShort 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

c
#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

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

c
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. */
Careful: Some popular interview snippets, like printf("%d %d", i++, i++) or i = i++ + ++i, have NO correct answer because they are undefined behaviour. The best answer is to say exactly that, explain why (unsequenced modifications), and show how to rewrite them. Interviewers value this more than a guessed number.

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.
Think of it like this: Answering a C interview question is like explaining a rule of cricket to a newcomer: one clear sentence, then a short example from a real match. Reciting the whole rule book helps nobody.
Tip: For every answer above, practise writing a 3–5 line example on paper. Many embedded interviews in India are still pen-and-paper or whiteboard rounds.

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

C interview questions

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.

c
/* "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)

Task 1 · Question

What does this print on a typical system? char s[10] = "hi"; printf("%zu %zu\n", sizeof s, strlen(s));

Task 2 · Code · Javascript

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

Task 3 · Practice

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

Question 1

1.On a little-endian machine, which byte of uint32_t x = 0x11223344 is stored first in memory?

Question 2

2.What is the best answer to "what does printf("%d %d", i++, i++) print"?

Question 3

3.unsigned int u = -1; on a system with 32-bit unsigned int, u is…

Question 4

4.Which is TRUE about the XOR swap trick?

Read more (free)

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

Answer: 2 2.5

Integer division then double division.

From Java interview questions

What is the difference between malloc and calloc?Related

Answer: calloc zeroes the memory and takes count and size

malloc leaves memory uninitialised.

From malloc, calloc and free

What is a dangling pointer?Related

Answer: A pointer to memory that was freed or went out of scope

Using it is undefined behaviour.

From NULL, dangling pointers and pointers to pointers

Which is NOT undefined behaviour?Related

Answer: Unsigned int overflow (it wraps around)

Unsigned arithmetic is defined to wrap modulo 2^bits.

From C++ interview questions and how to answer them

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. 1 more topic in “Embedded C, system calls and careers” before its exam: Projects and careers with C.
  2. Module 12 exam: Embedded C, system calls and careers · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. 60 topics left in the course before the final exam.

← Previous: C on Linux: files and processes with system calls