Interview Q&A · C · 10 questions in this topic
Data Types & Variables — C Interview Q&A
The C type system — sizes, signedness, conversions, and the fixed-width types portable code relies on. Part of Data Types & Operators · 202 questions in the C bank.
What are the fundamental data types and their typical sizes?
Answer
Core types are char, short, int, long, long long, float, double, _Bool/bool plus signed/unsigned modifiers. The standard guarantees minimum ranges (int is at least 16 bits) rather than exact sizes — exact sizes are implementation-defined, so portable code checks with sizeof or uses fixed-width types from <stdint.h>.
On typical 64-bit Linux/macOS: char 1, short 2, int 4, long 8, long long 8, float 4, double 8, pointers 8. On 64-bit Windows, long is 4 (LLP64).
Key points
- 1char — 1 byte; plain-char signedness is implementation-defined.
- 2int — at least 16 bits, almost always 32 bits today.
- 3Use int32_t / uint64_t from <stdint.h> when exact width matters.
Example
#include <stdio.h> #include <stdint.h> int main(void) { printf("%zu %zu %zu\n", sizeof(int), sizeof(long), sizeof(void*)); int32_t exact = 42; /* exactly 32 bits, everywhere */ return 0; }
What is the difference between signed and unsigned integers? What goes wrong mixing them?
Answer
Signed overflow is undefined behavior; unsigned arithmetic wraps modulo 2^N (defined but often still a logic bug). In mixed expressions the signed operand converts to unsigned (the usual arithmetic conversions), so -1 < 1u is false — -1 becomes a huge unsigned value.
Guideline: use signed ints for quantities by default, unsigned for bit manipulation and genuinely modular arithmetic, and compile with -Wsign-compare.
Example
#include <stdio.h> int main(void) { /* -1 converts to unsigned: comparison is false! */ if (-1 < 1u) printf("true\n"); else printf("false (signed converted to unsigned)\n"); return 0; }
What are the integer promotion and usual arithmetic conversion rules?
Answer
Small integer types (char, short, bit-fields, bool) promote to int (or unsigned int if int cannot hold them) before any arithmetic — so char + char is computed as int. Then the usual arithmetic conversions unify the operands to a common type: int to unsigned int, to long, to unsigned long, to float/double in a fixed rank order.
These silent promotions explain why `uint8_t a, b; a + b` is int, and why mixing signed/unsigned surprises even seniors.
Example
#include <stdint.h> uint8_t a = 200, b = 100; /* promoted to int BEFORE adding: result is int 300, not wrapped uint8_t */ int sum = a + b;
💡 Small types compute as int is the one-line rule behind most promotion questions.
What is the difference between `const` and `#define` constants?
Answer
const gives a typed, scoped, debugger-visible read-only object; #define performs untyped textual substitution with no scope or type checking. const respects the type system (a const int* cannot silently alias an int), while macros can double-evaluate arguments and leak names.
Prefer enum or const for constants; reserve macros for conditional compilation and unavoidable boilerplate.
Example
#define MAX_A 100 /* untyped text substitution */ const int MAX_B = 100; /* typed, scoped, debuggable */ enum { MAX_C = 100 }; /* true compile-time constant for array sizes */
What does `volatile` mean and when is it required?
Answer
volatile tells the compiler the object may change outside the visible program flow (hardware registers, memory-mapped I/O, signal-handler flags, setjmp contexts), so every access must actually hit memory — no caching in registers, no reordering or elimination.
It is not a threading tool: volatile gives no atomicity or memory ordering, so multithreaded sharing needs atomics (<stdatomic.h>) or mutexes, not volatile.
Example
/* memory-mapped hardware register: must re-read every time */ volatile unsigned *reg = (volatile unsigned *)0x40001000; unsigned status = *reg;
What does `static` mean in C? (It has three meanings.)
Answer
static is context-dependent: on a file-scope object/function it means internal linkage (visible only in that translation unit); on a block-scope variable it means static storage duration (persists across calls, zero-initialized); on a function parameter array it is a C99 minimum-length hint (void f(int a[static 10])) that compilers may use for optimization.
Interviewers ask this precisely because one keyword does three jobs — naming the context for each is the whole answer.
Key points
- 1File scope — internal linkage: private to this .c file.
- 2Local variable — persists across calls, initialized once.
- 3Array parameter — minimum element guarantee (C99 hint).
Example
static int calls = 0; /* file-local if outside, persistent if inside */ void f(void) { static int n = 0; /* survives across calls */ n++; }
💡 Enumerate all three without prompting — stopping after one or two is the common failure mode.
What is the difference between `char s[]`, `char *s`, and string literals?
Answer
char s[] = "hi" allocates a mutable array initialized from the literal. char *s = "hi" points at the (typically read-only) literal itself — writing through it is undefined behavior. String literals have static storage duration and may be pooled, so identical literals can share addresses.
In C, always declare literal pointers as const char* so the compiler rejects writes at compile time.
Example
char a[] = "hi"; /* mutable copy on the stack */ a[0] = 'H'; /* fine */ const char *p = "hi"; /* read-only view of the literal */ /* p[0] = 'H'; // undefined behavior (often segfault) */
What is the difference between `struct` and `union` (preview)?
Answer
A struct lays members out sequentially — size is at least the sum plus padding. A union overlays all members at offset zero — size is the largest member, and only one member holds a meaningful value at a time.
Structs model records; unions model alternatives (with a tag enum forming a tagged union) or type punning. Full detail lives in the structs section.
Example
struct Point { int x, y; }; /* both live side by side */ union Value { int i; float f; }; /* one OR the other */ union Value v; v.i = 5; /* v.f is now meaningless */
Local, global, static, register, extern — how do storage classes compare?
Answer
auto (the default for locals): automatic duration, garbage initial value. static locals/globals: static duration, zero-initialized, restricted visibility. extern: declaration referring to a definition elsewhere. register: a hint to keep the value in a CPU register (cannot take its address) — modern compilers ignore it and optimize better unaided.
Objects with static duration are zero-initialized; automatic ones are indeterminate until assigned.
Key points
- 1auto — stack local, indeterminate until assigned.
- 2static — persists, zero-initialized, restricted linkage/scope.
- 3extern — declares without defining.
- 4register — historical hint; address-of is forbidden.
Example
int g; /* static duration: zero-initialized */ void f(void) { int x; /* indeterminate — must assign before reading */ x = 1; }
What is `size_t`, `ptrdiff_t` and `uintptr_t`? When do you use each?
Answer
size_t (from <stddef.h>) is the unsigned type of sizes and counts — the return of sizeof and the right type for array indices and allocation sizes. ptrdiff_t is its signed counterpart for pointer differences. uintptr_t (from <stdint.h>, optional) is an unsigned integer guaranteed to round-trip a void* — for hashing or tagging pointers, never for arithmetic.
Using int for sizes invites overflow and sign-compare warnings on 64-bit platforms.
Example
#include <stddef.h> #include <stdint.h> /* right: */ for (size_t i = 0; i < n; i++) { /* ... */ } uintptr_t tag = (uintptr_t)ptr; /* round-trip storage, not arithmetic */
💡 size_t for sizes, ptrdiff_t for differences, uintptr_t for bit tricks on pointers.
Recommended resourcesAffiliate links · may earn us a commission