December Code

C for placements: interview questions by topic

C is the first programming language in most engineering syllabuses, and campus placement tests still assume it: the technical sections of many service-company tests include C output questions, and product companies use C to check that you understand what a computer does with your code. The questions are rarely about syntax. They sit where C hands you the machine directly: a pointer that moves by a whole element, a string that is only an array ending in a zero byte, a struct bigger than the sum of its members, a macro that expands into the wrong expression, and expressions whose result C deliberately leaves undefined.

Interviews add the questions behind the output: why a swap function needs pointers, where a local variable lives and when it dies, what malloc returns when it fails, how padding is decided, and what static means in each of its places. Knowing which answers the standard guarantees and which only your compiler gives you is what separates a careful answer from a memorised one.

The pages below take one topic each, with answers whose code was compiled and run with gcc, and one question exactly as the diagnostic asks it. The diagnostic draws from every C topic in the bank and names the ones to read first.

New to this? How to prepare for placements — the method — and what the readiness test measures.

C topics, one page each

8 of the 8 C topics in the bank have a page so far; the diagnostic draws from all 8.

  1. C data types and operators interview questions

    C data types and operators as interviews test them: type sizes, signed overflow, integer division, signed-unsigned conversions and precedence traps.

  2. C control flow interview questions

    C control flow as interviews test it: switch fall-through, the three loops, breaking out of nested loops, goto for cleanup and the ?: operator.

  3. C functions and recursion interview questions

    C functions as interviews test them: call by value, prototypes, returning arrays, qsort comparators, variadic functions and recursion on the stack.

  4. C pointer interview questions

    C pointers as interviews test them: & and *, pointer arithmetic, null, void and dangling pointers, const pointers and pointers to pointers.

  5. C arrays and strings interview questions

    C arrays and strings as interviews test them: arrays versus pointers, the null terminator, char[] versus char *, safe copying and 2D arrays.

  6. C structures and unions interview questions

    C structs and unions as interviews test them: struct versus union, padding and packing, typedef, the arrow operator, bit-fields and comparing structs.

  7. Dynamic memory allocation in C: interview questions

    Dynamic memory in C as interviews test it: stack versus heap, malloc, calloc, realloc and free, leaks, double frees, casting malloc and 2D arrays.

  8. C storage classes and macros interview questions

    C storage classes and the preprocessor as interviews test them: static, extern, auto and register, macros versus inline functions, const and volatile.

One question, exactly as the diagnostic asks it

From the C bank — 30 questions across 8 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.

Pointers · mediumCL-013

What does this C code print?

int a[] = {10, 20, 30};
int *p = a + 1;
printf("%d %d\n", *p + 1, *(p + 1));
  1. 130 30
  2. 221 21
  3. 311 20
  4. 421 30correct

p = a + 1 points at a[1], which holds 20. Unary * binds tighter than binary +, so *p + 1 reads 20 and adds 1, giving 21, while *(p + 1) first moves the pointer one element on, to a[2], and reads 30. 30 30 reads *p + 1 as *(p + 1). 21 21 reads *(p + 1) as adding 1 to the value. 11 20 assumes p starts at a[0]; it starts at a + 1. Note that p + 1 moves by one element, sizeof(int) bytes, not by one byte.

More on this topic: C pointer interview questions

Measure it

Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.

10 C questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.

By Harshit · updated