Interview Q&A · C · 10 questions in this topic
C Fundamentals — C Interview Q&A
The vocabulary every C interview opens with — what the language is, how source becomes an executable, and the program lifecycle. Part of C Fundamentals · 202 questions in the C bank.
What is C?
Answer
C is a general-purpose, statically typed, compiled procedural programming language developed by Dennis Ritchie at Bell Labs (first released in 1972). It provides low-level access to memory through pointers, a small standard library, and maps closely onto machine behavior — which is why operating systems (Unix, Linux, Windows kernels), embedded systems, compilers and language runtimes are written in it.
C is standardized by ISO — C89/C90, C99, C11, C17 (the current baseline) and C23. Its philosophy is minimal abstraction with programmer discipline: the language trusts you, so undefined behavior is your responsibility.
Example
#include <stdio.h> int main(void) { printf("Hello, interviewer!\n"); return 0; }
💡 Name one strength (portability, minimal runtime, hardware closeness) and one cost (manual memory, no safety net) — that frames every follow-up.
How does a C program go from source code to a running program?
Answer
Four stages: preprocessing (handles #include, #define, conditional compilation), compilation (each translation unit becomes assembly, then object code), linking (object files plus libraries combine into an executable), and loading/execution (the OS loader maps the binary into memory and starts main).
Most interview bugs live at a specific stage: a missing header is preprocessing, a type error is compilation, an undefined reference is linking, and a segfault is runtime.
Key points
- 1Preprocessing — expands #include files and macros into a translation unit.
- 2Compilation — each .c file compiles independently to a .o object file.
- 3Linking — resolves symbols across object files and libraries into one binary.
- 4Loading — the OS loads the binary; execution starts at main().
Example
/* a.c */ int add(int a, int b) { return a + b; } /* main.c */ int add(int a, int b); /* declaration */ int main(void) { return add(2, 3); } /* build: gcc -c a.c; gcc -c main.c; gcc a.o main.o -o app */
💡 Saying undefined reference is a linker error (not a compiler error) signals you have actually built multi-file C programs.
What are the valid signatures of main and what can it return?
Answer
The hosted-environment signatures are int main(void) and int main(int argc, char *argv[]). Returning 0 (or falling off the end of main, which implicitly returns 0 since C99) means success; returning non-zero (commonly EXIT_FAILURE from <stdlib.h>) signals failure to the OS.
argc counts the program name plus arguments, and argv[argc] is guaranteed to be NULL.
Example
#include <stdio.h> int main(int argc, char *argv[]) { printf("args: %d\n", argc); for (int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; }
💡 Mention that falling off main returns 0 implicitly — interviewers sometimes probe exactly that.
What are the compilation stages in detail (preprocessor, compiler, assembler, linker)?
Answer
The preprocessor handles #-directives: file inclusion, macro expansion, conditional compilation. The compiler proper translates preprocessed C into assembly. The assembler turns assembly into relocatable machine-code object files. The linker merges object files and libraries, resolving external symbols into the final executable or shared library.
Key points
- 1Preprocessor — textual transformation: #include, #define, #ifdef.
- 2Compiler — semantic analysis, optimization, assembly output.
- 3Assembler — assembly to relocatable machine code (.o).
- 4Linker — symbol resolution, relocation, library merging.
Example
/* inspect each stage with GCC: */ /* gcc -E main.c -o main.i // preprocessed */ /* gcc -S main.c -o main.s // assembly */ /* gcc -c main.c -o main.o // object file */ /* gcc main.o -o app // linked executable */
💡 Knowing gcc -E / -S / -c maps each flag to a pipeline stage and makes this answer concrete.
How does basic input/output work in C?
Answer
The <stdio.h> library provides stdin/stdout/stderr streams with printf (formatted output), scanf (formatted input), getchar/putchar (single characters) and fgets/fputs (line-oriented, safer). printf/scanf use format strings (%d, %s, %p) that must match the argument types exactly — a mismatch is undefined behavior.
For robust input prefer fgets plus sscanf/strtol over bare scanf, which mishandles malformed input and overflows %s without a width.
Example
#include <stdio.h> int main(void) { char name[64]; printf("Enter name: "); if (fgets(name, sizeof name, stdin)) printf("Hello, %s", name); return 0; }
💡 Mention fgets over gets (removed in C11 — it could never be used safely) to show you know the history.
What is undefined behavior, and why do C interviews obsess over it?
Answer
Undefined behavior (UB) means the standard imposes no requirements — the program may crash, corrupt data, or appear to work. Classic causes: signed integer overflow, null/dangling pointer dereference, use-after-free, buffer overflows, data races, use of uninitialized values, and strict-aliasing violations.
Interviews obsess because optimizing compilers assume UB never happens and optimize accordingly — so UB can corrupt even the code before the bug, and sanitizers (ASan/UBSan) exist precisely to catch it.
Key points
- 1Unspecified behavior — valid options, implementation chooses (e.g. order of some evaluations).
- 2Implementation-defined — implementation must document its choice (e.g. sizeof(int)).
- 3Undefined — no guarantees at all; the dangerous one.
Example
int x = INT_MAX; /* x + 1; // signed overflow: undefined behavior */ int *p = NULL; /* *p = 1; // null dereference: undefined behavior */
💡 Naming ASan/UBSan as your debugging tool for UB shows production experience, not just standardese.
What is the difference between declaration and definition?
Answer
A declaration introduces a name and its type without allocating storage or providing a body — it tells the compiler the entity exists. A definition additionally provides the body or allocates storage. A program may declare an entity many times but must define non-inline functions and objects exactly once.
Headers normally carry declarations; exactly one .c file carries the definition.
Example
/* declaration (usually in a header): */ int add(int a, int b); extern int counter; /* definition (in exactly one .c file): */ int add(int a, int b) { return a + b; } int counter = 0;
💡 Connect this to linker errors: undefined reference means a declaration was seen but no definition was linked in.
What is the difference between #include <...> and #include "..."?
Answer
Both paste a file into the translation unit, but the search order differs: the quoted form searches the including file's directory first (for project headers) and then the include path, while the angle-bracket form searches only the configured include paths (for system and library headers).
Convention: quotes for your own headers, angle brackets for standard/third-party headers. Include guards prevent double inclusion.
Example
#include <stdio.h> /* standard library: angle brackets */ #include "sensor.h" /* project header: quotes */ #ifndef SENSOR_H #define SENSOR_H /* ... */ #endif
💡 Mention include guards in the same breath — interviewers often chain these two questions.
What is the difference between C and C++?
Answer
C is procedural with manual everything: no classes, no references, no overloading, no templates, no exceptions — polymorphism via function pointers and void*. C++ adds all of those plus stronger type checking (implicit void* conversions allowed in C are errors in C++).
Idiomatic C leans on structs plus function-pointer tables, explicit size arguments, opaque pointers for encapsulation, and disciplined ownership conventions instead of destructors.
Key points
- 1OOP — C++ has classes; C uses structs + function pointers.
- 2Generics — C++ templates vs C macros and void* tricks.
- 3Errors — C++ exceptions vs C return codes and errno.
- 4Types — C++ is stricter; C permits more implicit conversions.
💡 End with one idiomatic C pattern (opaque pointer) to show you write real C, not C++-without-classes.
Is C statically or dynamically typed? What does that imply?
Answer
C is statically typed: every variable and expression has a type fixed at compile time, and the compiler checks operations before the program runs. But C is also weakly typed compared to C++ — implicit conversions (int to char, void* to anything) flow freely, so the compiler catches far less than you might hope.
The trade-off is speed and simplicity against safety: no runtime type checks in optimized builds, so -Wall -Wextra plus sanitizers carry the checking load the language skips.
Example
int x = 5; /* x = "hello"; // compile error: no such conversion */ void *p = &x; /* implicit conversion to void* is fine in C */ int *q = p; /* also fine in C — an error in C++ */
Recommended resourcesAffiliate links · may earn us a commission