1. Which header file contains definitions for fixed-width integer types such as uint32_t and int64_t?
Answer: <stdint.h>.
C99 introduced <stdint.h> with portable, fixed-width integer typedefs: int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, plus intptr_t, uintptr_t, and the intN_t “at least N bits” variants. This gives you exact-size types regardless of platform (where plain int is 16 or 32 or 64 bits depending on the architecture).
(C++ uses <cstdint> for the same types.) The interview answer: <stdint.h> (C99) defines the fixed-width integer types.
Answer:
<stdint.h>.
C99 introduced <stdint.h> with portable, fixed-width integer typedefs: int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, plus intptr_t, uintptr_t, and the intN_t “at least N bits” variants. This gives you exact-size types regardless of platform (where plain int is 16 or 32 or 64 bits depending on the architecture).
(C++ uses <cstdint> for the same types.) The interview answer: <stdint.h> (C99) defines the fixed-width integer types.
2. What is the purpose of the #pragma once directive?
Answer: A header guard optimization — it prevents a header file from being included more than once in a single compilation unit.
#pragma once is a preprocessor directive (supported by virtually all modern compilers) that tells the compiler: “only include this file once per translation unit, no matter how many times it’s #included.”
It’s an alternative to the classic include guards:
#ifndef MY_HEADER_H
#define MY_HEADER_H
/* ... */
#endif
#pragma once achieves the same goal with less boilerplate and fewer typo risks (a mismatched #define name in the guard defeats it). Its trade-off: it’s not part of the ISO C standard (hence #pragma), so its support is a compiler-extension convention — though in practice every mainstream compiler supports it. The interview answer: a modern, concise header guard preventing multiple inclusion of a header in one compilation unit.
Answer:
A header guard optimization — it prevents a header file from being included more than once in a single compilation unit.
#pragma once is a preprocessor directive (supported by virtually all modern compilers) that tells the compiler: “only include this file once per translation unit, no matter how many times it’s #included.”
It’s an alternative to the classic include guards:
#ifndef MY_HEADER_H
#define MY_HEADER_H
/* ... */
#endif
#pragma once achieves the same goal with less boilerplate and fewer typo risks (a mismatched #define name in the guard defeats it). Its trade-off: it’s not part of the ISO C standard (hence #pragma), so its support is a compiler-extension convention — though in practice every mainstream compiler supports it. The interview answer: a modern, concise header guard preventing multiple inclusion of a header in one compilation unit.
3. What is the output of the macro expansion SQUARE(1 + 2) defined as #define SQUARE(x) x * x?
Answer: 5 — the classic macro-precedence trap.
Macros are pure textual substitution with no expression-awareness. SQUARE(1 + 2) expands to:
1 + 2 * 1 + 2
Applying normal C operator precedence (* binds tighter than +):
1 + (2 * 1) + 2 = 1 + 2 + 2 = 5
The programmer expected (1 + 2) × (1 + 2) = 9, but got 5. The fix is to parenthesize the parameters in the macro definition:
#define SQUARE(x) ((x) * (x))
(Full parenthesization also protects against issues when the result is used in further expressions.) The interview answer: 5 — macro substitution ignores precedence, giving 1 + 2 * 1 + 2.
Answer:
5 — the classic macro-precedence trap.
Macros are pure textual substitution with no expression-awareness. SQUARE(1 + 2) expands to:
1 + 2 * 1 + 2
Applying normal C operator precedence (* binds tighter than +):
1 + (2 * 1) + 2 = 1 + 2 + 2 = 5
The programmer expected (1 + 2) × (1 + 2) = 9, but got 5. The fix is to parenthesize the parameters in the macro definition:
#define SQUARE(x) ((x) * (x))
(Full parenthesization also protects against issues when the result is used in further expressions.) The interview answer: 5 — macro substitution ignores precedence, giving 1 + 2 * 1 + 2.
4. What is the purpose of <stdbool.h> introduced in C99?
Answer: It defines bool (an alias for the built-in _Bool), plus true (1) and false (0).
C99 introduced the native _Bool type. <stdbool.h> provides the friendly macros:
bool→_Booltrue→1false→0
#include <stdbool.h>
bool isReady = true;
C’s _Bool is a real type: it stores only 0 or 1, and any non-zero value assigned to it converts to 1. So bool b = 42; yields b == 1. Before C99/C23 code had to fake booleans with int/typedef int bool;. The interview answer: defines bool (=_Bool), true (1), and false (0).
Answer:
It defines bool (an alias for the built-in _Bool), plus true (1) and false (0).
C99 introduced the native _Bool type. <stdbool.h> provides the friendly macros:
bool→_Booltrue→1false→0
#include <stdbool.h>
bool isReady = true;
C’s _Bool is a real type: it stores only 0 or 1, and any non-zero value assigned to it converts to 1. So bool b = 42; yields b == 1. Before C99/C23 code had to fake booleans with int/typedef int bool;. The interview answer: defines bool (=_Bool), true (1), and false (0).
5. What is the purpose of header guards in C (#ifndef HEADER_H …)?
Answer: They prevent duplicate declaration errors when the same header is included more than once in one compilation unit.
Headers are included textually. If a.h includes c.h and b.h also includes c.h, then including both in a .c file would paste c.h’s contents twice — re-declaring structs, typedefs, enums, and macros → compilation errors. The guard prevents that:
// c.h
#ifndef C_H
#define C_H
struct Point { int x, y; }; // only seen once per TU
#endif
How it works: the first inclusion defines C_H and pastes the body; any later inclusion sees C_H already defined and skips the whole block. (#pragma once is the modern shorthand.) The interview answer: guards make the header idempotent, so multiple #includes of the same file cause no re-declaration errors.
Answer:
They prevent duplicate declaration errors when the same header is included more than once in one compilation unit.
Headers are included textually. If a.h includes c.h and b.h also includes c.h, then including both in a .c file would paste c.h’s contents twice — re-declaring structs, typedefs, enums, and macros → compilation errors. The guard prevents that:
// c.h
#ifndef C_H
#define C_H
struct Point { int x, y; }; // only seen once per TU
#endif
How it works: the first inclusion defines C_H and pastes the body; any later inclusion sees C_H already defined and skips the whole block. (#pragma once is the modern shorthand.) The interview answer: guards make the header idempotent, so multiple #includes of the same file cause no re-declaration errors.
6. What is the value of NULL in standard C header files?
Answer: An implementation-defined null pointer constant — typically ((void*)0) or 0.
NULL is a macro (defined in <stddef.h>, <stdio.h>, etc.) representing a null pointer constant. Common definitions:
((void *)0)— a void pointer to address 0.0— the integer zero constant, which converts to a null pointer in pointer contexts.
Either way, NULL compares equal to any null pointer and is not a valid address to dereference. The standard requires a null pointer constant to be an integer constant expression with value 0, or such an expression cast to void*. In C++, the idiom is nullptr (since C++11). The interview answer: an implementation-defined null pointer constant, commonly ((void*)0) or plain 0.
Answer:
An implementation-defined null pointer constant — typically ((void*)0) or 0.
NULL is a macro (defined in <stddef.h>, <stdio.h>, etc.) representing a null pointer constant. Common definitions:
((void *)0)— a void pointer to address 0.0— the integer zero constant, which converts to a null pointer in pointer contexts.
Either way, NULL compares equal to any null pointer and is not a valid address to dereference. The standard requires a null pointer constant to be an integer constant expression with value 0, or such an expression cast to void*. In C++, the idiom is nullptr (since C++11). The interview answer: an implementation-defined null pointer constant, commonly ((void*)0) or plain 0.
Premium Content
Unlock Preprocessor & Macros and all premium lessons with a subscription.
From ₹199.99/year — See plans