Before compilation, the preprocessor handles #include, #define, and conditional compilation. Lines starting with # are preprocessor directives.
#include
#include <stdio.h> /* system header */
#include "mylib.h" /* project header */
#define macros
#define MAX_SIZE 100
#define SQUARE(x) ((x) * (x))
Prefer const variables and inline functions for new code; macros remain common in headers and embedded configs.
Include guards
#ifndef MYLIB_H
#define MYLIB_H
/* declarations */
#endif
Important interview questions and answers
- Q: Macro vs const?
A: Macros are text substitution without type checking;constvariables are typed and debuggable. - Q: Why include guards?
A: Prevent duplicate definitions when headers are included from multiple paths.
Self-check
- What does #include do?
- Why wrap macro arguments in parentheses?
Interview prep
- Macro vs const?
Macros are text substitution without type checking;
constvariables are typed, scoped, and debuggable.- Include guard purpose?
Prevent the same header from being processed twice, which would duplicate struct tags and function prototypes.