Skip to content
  • C Interview Questions · C

C Interview Questions

LearnViaNotes @learnvianotes· 8 min read

The questions that come up again and again in C and embedded interviews — each with a crisp answer. Most are covered in depth in the earlier topics.

Output prediction warm-ups

C — compiled with -Wno-sign-compare to allow the trap
#include <stdio.h>
#include <string.h>

int main(void) {
    char s[] = "interview";
    char *p = s;
    printf("%zu %zu\n", sizeof s, strlen(s));        /* 10 9 */
    printf("%c %c\n", *p + 1, *(p + 1));             /* 'i'+1 = 'j', then 'n' */

    int a[] = {1, 2, 3, 4, 5};
    int *q = a + 1;
    printf("%d %d %td\n", q[2], *(a + 4), &a[4] - q);

    unsigned int u = 5;
    int i = -10;
    printf("%s\n", u + i > 5 ? "big" : "small");     /* i converts to unsigned */

    int x = 10;
    printf("%d\n", x >> 1 << 2);                     /* (10 >> 1) << 2 = 20 */
    printf("%d\n", 3 == 3.0);                        /* 3 converts to double */
    printf("%d\n", 'A' + 1);
    return 0;
}
Output
10 9
j n
4 5 3
big
20
1
66

Language fundamentals

What are the storage classes in C?

What is the difference between declaration and definition?

What is a volatile variable? Give examples.

Explain const int *p, int *const p and const int *const p.

What is the difference between ++i and i++ in a for loop?

What are sequence points and why do they matter?

Pointers and memory

What is the difference between malloc and calloc? What does realloc do?

What are dangling pointers, wild pointers and memory leaks?

What is a NULL pointer vs a void pointer?

Can you return a pointer to a local variable?

What is the difference between arrays and pointers?

How do you find the size of an array passed to a function?

What is memory alignment and structure padding?

Explain stack vs heap.

Classic coding questions

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

/* 1. reverse words in place: "the sky is blue" -> "blue is sky the" */
static void rev(char *a, char *b) { while (a < b) { char t = *a; *a++ = *b; *b-- = t; } }
void reverse_words(char *s) {
    size_t n = strlen(s);
    if (n == 0) return;
    rev(s, s + n - 1);                               /* reverse whole string */
    for (char *w = s; *w; ) {                        /* then each word */
        while (*w == ' ') w++;
        char *e = w;
        while (*e && *e != ' ') e++;
        if (e > w) rev(w, e - 1);
        w = e;
    }
}

/* 2. endianness check */
int is_little_endian(void) { unsigned x = 1; return *(unsigned char *)&x == 1; }

/* 3. my_atoi with sign and overflow saturation */
int my_atoi(const char *s) {
    long v = 0;
    int sign = 1;
    while (*s == ' ') s++;
    if (*s == '-' || *s == '+') sign = (*s++ == '-') ? -1 : 1;
    while (*s >= '0' && *s <= '9') {
        v = v * 10 + (*s++ - '0');
        if (v > 2147483648L) break;
    }
    v *= sign;
    if (v > 2147483647L) return 2147483647;
    if (v < -2147483648L) return (int)-2147483648L;
    return (int)v;
}

/* 4. count set bits, reverse bits of a byte */
unsigned char reverse_byte(unsigned char b) {
    unsigned char r = 0;
    for (int i = 0; i < 8; i++) { r = (unsigned char)((r << 1) | (b & 1)); b >>= 1; }
    return r;
}

int main(void) {
    char s[] = "the sky is blue";
    reverse_words(s);
    printf("%s\n", s);
    printf("little endian: %d\n", is_little_endian());
    printf("%d %d %d %d\n", my_atoi("  -42"), my_atoi("+17abc"), my_atoi("99999999999"), my_atoi("-99999999999"));
    printf("0x%02X\n", reverse_byte(0x01));
    return 0;
}
Output
blue is sky the
little endian: 1
-42 17 2147483647 -2147483648
0x80

Tricky questions

What does printf return?

What is the output of sizeof on an expression with side effects, e.g. sizeof(i++)?

How can you swap two variables without a temporary?

What is the difference between #include guards and #pragma once?

Why is gets() dangerous and what replaces it?

What is a memory-mapped register and how do you access it in C?

What happens when you write int *p = malloc(10); p[10] = 1;?

Does C use name mangling?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report