Autocoder

C Language Interview Questions and Answers

Last updated 2026-11-19

C is still the language behind operating systems, embedded firmware and the core of many databases, so interviews for systems, embedded and many fresher roles include it. The questions here go beyond syntax: storage classes, how structs are laid out, what the preprocessor really does, and where C leaves behaviour undefined.

Answers are written to be spoken, with a short example where it helps. Output claims were checked with gcc on a 64-bit Linux system. For pointer-specific practice, see C pointers questions; for trace-the-output drills, C programming output questions. If the same interview asks DBMS or OS, how to crack a DBMS interview covers that side.

StandardC99 and C11 behaviour, gcc
TopicsStorage, structs, preprocessor, memory, strings
AudienceFreshers and embedded roles

What C interviews emphasise

Embedded flavour

For embedded roles expect volatile, bit fields, bit masking, endianness and memory-mapped registers. A good answer to "what is volatile" mentions hardware registers and interrupt-modified flags, and also says that volatile is not a synchronisation tool.

C Language Interview Questions and Answers: questions with worked answers

  1. 1

    What are the storage classes in C and what does each do?

    easy

    auto is the default for locals: stack storage, scope of the block. register hints that a variable should live in a CPU register (you cannot take its address). static on a local gives it a lifetime of the whole program, initialised once; static on a global or function limits it to the file (internal linkage). extern declares a name defined elsewhere, giving external linkage. A static local with no initialiser starts at zero, which is the reason counters inside functions work.

    c
    int counter(void) { static int n; return ++n; }
    // counter() returns 1, then 2, then 3
  2. 2

    What is the difference between a declaration and a definition?

    easy

    A declaration introduces a name and its type ("extern int x;" or a function prototype) without allocating storage. A definition allocates the storage or provides the function body ("int x;" or the function with its body). An object can be declared many times but must be defined exactly once in the program, which is why headers hold declarations and one .c file holds the definition. Defining a variable in a header included twice causes a multiple-definition link error.

  3. 3

    What is the difference between a struct and a union? What are their sizes?

    medium

    A struct stores each member at its own offset; a union overlays all members at the same address, so its size is that of the largest member (with alignment). Only the last-written member is meaningful, though reading a different member of the same size to inspect bytes is allowed in C (type punning through unions is defined in C, unlike C++). Structs add padding for alignment: struct { char c; int i; } is 8 bytes on common platforms.

    c
    struct S { char c; int i; };      // sizeof 8 (3 bytes padding)
    union  U { char c; int i; };      // sizeof 4
    printf("%zu %zu\n", sizeof(struct S), sizeof(union U)); // 8 4
  4. 4

    What is structure padding and how can you reduce it?

    medium

    Each member is aligned to a multiple of its own size, and the struct size is rounded to a multiple of its largest alignment, so the compiler inserts unused bytes. Declaring members from largest to smallest usually removes most padding: struct { int i; char a; char b; } is 8 while struct { char a; int i; char b; } is 12. #pragma pack and __attribute__((packed)) remove padding but make unaligned access slower or fault on some processors, and they hurt portability across protocols.

  5. 5

    What does the volatile keyword do, and what does const volatile mean?

    medium

    volatile tells the compiler that a variable can change outside normal program flow, such as a hardware status register, a variable changed by an interrupt handler or a signal handler, so each access must really read or write memory and must not be cached in a register or optimised away. const volatile is a read-only status register: the program must not write it, but its value may change. volatile does not make operations atomic and is not a substitute for locks.

  6. 6

    What is the difference between #define and const?

    easy

    #define is a textual substitution by the preprocessor: it has no type, no scope and no address, and arguments in function-like macros can be evaluated twice. A const variable has a type, obeys scope rules, can be seen in a debugger and can have its address taken. Prefer const (or enum for integer constants) in modern C. Always parenthesise macro parameters and the whole expansion, and avoid macros with side-effecting arguments such as MAX(i++, j).

  7. 7

    What are include guards and why are they needed?

    medium

    A header included twice in one translation unit, directly or through other headers, can produce duplicate definitions of types or functions. An include guard wraps the file in #ifndef NAME_H / #define NAME_H / #endif so that the second inclusion expands to nothing. #pragma once does the same on most compilers but is not standard. Guards stop duplication within one translation unit only; across translation units you still need declarations in headers and definitions in one source file.

    c
    #ifndef POINT_H
    #define POINT_H
    typedef struct { int x, y; } Point;
    int dist2(Point a, Point b);
    #endif
  8. 8

    Compare malloc, calloc, realloc and free. What are common mistakes?

    medium

    malloc(n) returns n uninitialised bytes. calloc(count, size) returns zeroed memory and detects overflow in count*size. realloc(p, n) resizes a block, possibly moving it: assign the result to a temporary first, or you lose the original pointer if it returns NULL. free releases a block and must be given exactly a pointer returned by these functions (or NULL, which is a safe no-op). Common errors: not checking for NULL, leaks, double free, use after free, and writing past the end of the block.

    c
    int *tmp = realloc(p, n * sizeof *p);
    if (!tmp) { free(p); return -1; }   // do not do p = realloc(p, ...)
    p = tmp;
  9. 9

    How are strings represented in C? What is wrong with gets and strcpy?

    medium

    A string is a char array ending with a zero byte, so a string of n characters needs n+1 bytes. The library functions do not know the buffer size. gets cannot limit input and was removed in C11, so use fgets. strcpy and strcat copy until the terminator and overflow a short destination, which is the classic buffer overflow, so use snprintf, or strncpy with explicit termination, or track lengths yourself. Also remember strlen is O(n) and returns size_t.

  10. 10

    What is the difference between ++i and i++ and what is a sequence point?

    medium

    ++i increments and yields the new value; i++ yields the old value and then increments. A sequence point (more precisely, a sequencing relation in C11) guarantees that all side effects of earlier evaluation are complete. Modifying a variable twice, or modifying and reading it, without sequencing in between is undefined: i = i++ + ++i and a[i] = i++ have no defined result. Sequence points occur at the end of a full expression, at && || ?: and comma operators, and before a function is called.

  11. 11

    What happens in integer promotion and signed/unsigned comparison?

    medium

    Operands narrower than int (char, short) are promoted to int before arithmetic, and when a signed and an unsigned int of the same rank meet, the signed value converts to unsigned. So -1 < 1u is false, because -1 becomes 4294967295. A loop such as for (unsigned i = n; i >= 0; i--) never ends, since an unsigned value is never negative. Use signed types for counters that can go below zero and compile with -Wsign-compare.

  12. 12

    What is endianness and how can you check it in C?

    hard

    Endianness is the byte order of multi-byte values in memory: little-endian stores the least significant byte at the lowest address (x86 and most ARM configurations), big-endian the opposite (network byte order). Check by examining the first byte of a known integer through a char pointer. It matters when writing binary files, network protocols and talking to hardware; use htonl and ntohl for network order.

    c
    unsigned int v = 1;
    if (*(unsigned char*)&v == 1) puts("little endian");
    else puts("big endian");
  13. 13

    What are bit fields and how do you set, clear and toggle a bit?

    medium

    Use masks with bitwise operators: set with n |= (1u << k), clear with n &= ~(1u << k), toggle with n ^= (1u << k), test with (n >> k) & 1. Bit fields declare members with a width inside a struct (unsigned flag : 1;), but their layout, ordering and signedness are implementation defined, so masks are safer for portable or register-mapped code. Use unsigned types with shifts: shifting a signed value into the sign bit is undefined. More at bit manipulation questions.

  14. 14

    What is the difference between a macro and an inline function?

    hard

    A macro is expanded textually before compilation, so it has no type checking and may evaluate arguments more than once. An inline function (C99) is a real function the compiler may expand in place: arguments are evaluated once, types are checked and it can be stepped through in a debugger. The inline keyword is only a hint, and in C it needs one external definition if the compiler decides not to inline, usually provided as extern inline in one source file.

  15. 15

    How does compilation proceed from a .c file to an executable?

    medium

    Preprocessing expands #include, #define and conditional directives. Compilation turns each translation unit into assembly and then an object file. The linker combines object files and libraries, resolves external symbols and produces the executable (an "undefined reference" error comes from here, while a missing prototype or type error comes from compilation). With gcc, -E stops after preprocessing, -S after assembly generation and -c after producing the object file.

Frequently asked questions

Is C still asked in fresher interviews?

Yes, especially in embedded, systems and many campus technical rounds, where C basics, pointers and memory are used to test fundamentals.

What is the difference between C and C++ in interviews?

C has no classes, templates, references or RAII, and its strings and memory are fully manual. See C++ interview questions for the C++ side.

How do I check my answers for C questions?

Compile with gcc -Wall -Wextra -fsanitize=address,undefined and read the warnings. Many tricks in interview questions are exactly what those flags report.

What should I revise right before a C test?

Operator precedence, integer promotion, pointer arithmetic, string functions and the storage classes, in that order.

Related guides

More in Programming Language Interview Questions by Language

See all in this section