C interview questions: pointers, memory, undefined behavior and more

Reviewed by Mark Dickie · Last updated

5 questions on this page you can answer and see marked on the spot. Go to the first one

C is a compiled, statically typed systems language that gives you direct access to memory through pointers and leaves safety checks to the programmer. Interviews focus on pointers and arrays, manual memory with malloc and free, storage and lifetime rules, and undefined behavior. If you can explain why a snippet misbehaves and what the standard guarantees, you are covering most of what gets asked.

What does a C interview test?

Expect short code reads, where you predict output or spot the bug, plus questions on how C maps to the machine. The table lists the main areas and a typical probe for each.

AreaTypical probe
Pointers and arraysWhy sizeof(arr) differs inside a function that receives int arr[]
Dynamic memoryWhat happens on double free, or on realloc returning NULL
Storage durationDifference between static locals, automatic variables and heap objects
Undefined behaviorSigned integer overflow, out-of-bounds reads, use after free
PreprocessorWhy #define SQUARE(x) x*x breaks with SQUARE(a+1)
StringsForgetting the null terminator, strcpy versus strncpy
QualifiersWhat const char *p versus char *const p lets you change

How should I prepare for C interview questions?

Work through these steps in order, and write real code for each one.

  1. Write a linked list with insert and delete, and run it under Valgrind or AddressSanitizer to catch leaks.
  2. Implement your own strlen, strcpy and memcpy, then note where overlapping buffers make memcpy unsafe and memmove necessary.
  3. Read declarations like int (*fp)(const char *) aloud, then build a small callback table with function pointers.
  4. Print sizeof for structs and work out where padding and alignment come from.
  5. Compile with -Wall -Wextra and explain each warning you get, especially around uninitialized variables and implicit conversions.

Key facts

  • Tarmac has 92 C interview questions on this topic, 10 of them on this page, at difficulty 1–4 of 5.
  • Tarmac tracked 90 job postings asking for C in November 2026.
  • Roles asking for C advertise a median base salary of £57,500, across 13 job postings as of November 2026.
  • Tarmac last reviewed these C interview questions on 5 December 2026.

At a glance

Questions10 shown · 92 in the bank
Difficulty1–4 of 5
FormatsTrue / false, Find the bug, Multiple choice, Short answer, Ordering, Fill in the blank, Code output, Multiple answer, Flashcard

What you'll review

  1. c buffer overflows
  2. c compilation stages
  3. c array decay
  4. c null terminated strings

Practice questions

Try one before you open the answer. Pick an option and press Check; it's marked on the spot.

In C, the standard library function gets() was removed from the language in the C11 standard because it cannot limit how many characters it writes into the destination buffer.#

Options

Show answer

The C11 standard removed gets() from C. The function has no way to know the size of the destination buffer, so input that is too long always overflows it. It was deprecated in C99 TC3 and removed in C11. fgets(), which takes a buffer-size argument, is the standard replacement.

Why:

gets(char *s) takes no buffer-size argument, so it keeps writing characters until it reaches a newline, however small the buffer is. Any input longer than the buffer overflows it. C99 TC3 deprecated the function and C11 removed it. fgets(buf, sizeof buf, stdin) replaces it because it takes a size limit.

Given char buf[5];, the call strcpy(buf, "hello"); is safe because the string "hello" has exactly 5 characters.#

Options

Show answer

Copying "hello" into char buf[5] with strcpy is not safe. It overflows the buffer by one byte, because a C string also needs a null terminator: "hello" takes 6 bytes. strcpy copies that terminator, so it writes past the end of the array, which is undefined behavior.

Why:

A C string ends with a null terminator, so "hello" takes up 6 bytes: 5 letters plus '\0'. strcpy copies the terminator as well, so it writes one byte past the end of the 5-byte buf. That is a buffer overflow and undefined behavior. The buffer needs at least 6 bytes.

This program is meant to print the area and circumference of a circle. It fails to compile with an error that PI is undeclared. The name PI is used on lines 7 and 8, and every use is intended. Which line contains the bug?#

#include <stdio.h>
#define Pi 3.14159

int main(void) {
    double r = 2.0;
    printf("Area:\n");
    printf("%f\n", PI * r * r);
    printf("%f\n", 2 * PI * r);
    return 0;
}
Show answer

The bug is on line 2.

Why:

Macro names in C are case-sensitive. Line 2 defines Pi, but lines 7 and 8 both use PI, and those uses are intended. No macro named PI exists, so the preprocessor leaves it alone and the compiler reports an undeclared identifier. Two uses agree on PI and only the definition differs, so the single faulty line is line 2. The fix is #define PI 3.14159.

On a typical 64-bit platform (8-byte pointers, 4-byte int), what does this program print?#

Options

Show answer

The program prints 8, the size of a pointer. In C, a function parameter declared as an array, such as int a[10], is adjusted to int *a. So sizeof a inside the function measures a pointer, not the 40-byte array the caller passed. Array sizes have to be passed as a separate argument.

Why:

A function parameter declared as int a[10] is adjusted by the compiler to int *a, and the size written in the brackets is ignored. Inside f, sizeof a therefore gives the size of a pointer, which is 8 bytes on this platform. The value 40 would be sizeof arr measured in main, where arr is still a real array. 10 is the element count, and 4 is sizeof(int).

A developer writes char dst[8]; strncpy(dst, src, sizeof dst); printf("%s", dst); thinking strncpy makes the copy safe. If src is 8 or more characters long, what goes wrong, and how do you fix it?#

Show answer

When src has 8 or more characters, strncpy copies exactly 8 bytes and does not add a null terminator. dst is then not a valid string, so printf keeps reading past the end of the buffer (an out-of-bounds read). To fix it, copy at most sizeof dst - 1 bytes and set dst[sizeof dst - 1] = '\0' yourself. You can also use snprintf, which always null-terminates.

Why:

strncpy only adds a null terminator if the source is shorter than n. When the source fills the buffer, printf reads past the end looking for one, which is undefined behaviour. Terminating the buffer yourself, or using snprintf, fixes this.

Given char s[] = "Hi!"; in C, order the bytes stored in the array s from index 0 to the last index.#

Put these in order

Show answer

The array char s[] = "Hi!"; holds four bytes in this order: 'H', 'i', '!', then the null terminator '\0'. The compiler appends the terminator automatically, so sizeof s is 4 while strlen(s) is 3.

Why:

A string literal initializer copies each character in order and appends a terminating null byte, so s has size 4: 'H' at index 0, 'i' at 1, '!' at 2, and '\0' at index 3. strlen(s) is 3 because it counts characters before the terminator, while sizeof s is 4.

With GCC, running gcc -E main.c stops after the _____ stage, gcc -S main.c stops after compilation and produces a .s _____ file, and gcc -c main.c stops after assembling and produces a .o _____ file.#

Show answer

With GCC, running gcc -E main.c stops after the preprocessing stage, gcc -S main.c stops after compilation and produces a .s assembly file, and gcc -c main.c stops after assembling and produces a .o object file.

Why:

GCC builds in four stages: preprocessing, compilation, assembly and linking. -E prints the preprocessed source and stops. -S stops after compilation and writes assembly code to a .s file. -c runs the assembler too and writes a relocatable object file (.o), but it does not link.

What does this program print?#

#include <stdio.h>
#include <string.h>

int main(void) {
    char s[] = "hello\0world";
    printf("%zu %zu\n", sizeof s, strlen(s));
    return 0;
}
Show answer
12 5
Why:

The literal holds 11 visible characters (h,e,l,l,o,\0,w,o,r,l,d) and the compiler adds its own terminating null, so the array s gets 12 bytes and sizeof s is 12. strlen counts characters only up to the first null byte, which comes right after "hello", so it returns 5. The program prints 12 5.

In standard C (C11/C17), given int a[10]; on a typical platform where sizeof(int) == 4 and sizeof(int*) == 8, which of the following expressions do NOT cause a to decay to a pointer to its first element? Select all that apply.#

Options

Pick every one that applies.

Show answer

The expressions sizeof a and &a do not cause the array a to decay. C11 6.3.2.1p3 exempts the operands of sizeof and unary & (and string literals initializing arrays) from array-to-pointer conversion. In a + 0, *a and a[3], the array decays to int *.

Why:

C11 6.3.2.1p3 lists the exceptions to array-to-pointer conversion: the operand of sizeof, the operand of unary &, and a string literal used to initialize an array. So sizeof a yields 40 (the whole array) and &a has type int (*)[10]. In a + 0, *a, and a[3] (defined as *(a + 3)), the array is an ordinary operand and decays to int *, so those do not avoid decay.

Why is strncpy(dst, src, sizeof dst); not a safe drop-in fix for strcpy buffer overflows in C?#

Show answer

Because strncpy does not guarantee a NUL terminator. When strlen(src) >= n (including exactly equal), the source has no room for its NUL, so strncpy copies n bytes, truncates the string, and leaves dst unterminated. Later calls like strlen, printf("%s") or strcat then read past the end of the buffer (an over-read, or an overflow if they write). Fix: use strncpy(dst, src, sizeof dst - 1) and set dst[sizeof dst - 1] = '\0', or use snprintf(dst, sizeof dst, "%s", src), which always terminates when the size is nonzero. Check its return value: if it is >= sizeof dst, the output was truncated. Side note: when src is shorter than n, strncpy pads the rest with zeros, which costs time.

Why:

strncpy was designed for fixed-width record fields, not for C strings. When the source length is n or more, it fills the destination with n bytes of source and writes no terminator, so the result is an unterminated, truncated buffer. Manual termination, or snprintf with a return-value check, avoids the latent over-read.

Related interview questions

Job market

See c salaries and hiring demand from live job postings.

The other 82 questions

This page shows 10 and marks what you pick. That's as far as a page can go. A free account opens the other 82 and keeps every answer. What you miss comes back until it's right: after a day, then at longer gaps.

Start with this topic

Free · the whole bank · 100 answers per 30 days, each marked with written feedback

Interview booked? Practise the job ad instead

What moved, monthly

One email a month when the bulletin comes out: what moved in the markets we track, and the new question topics we published. Confirm your address to join. Unsubscribe any time.