Preprocessor Directives and Standard Library Overview
This domain tests the text-substitution phase that runs before compilation, and your awareness of which standard header supplies which function.
How the preprocessor works
Directives begin with # at the start of a line and take no semicolon. #include <stdio.h> searches the system directories; #include "my.h" searches the current directory first. #define PI 3.14159 is an object-like macro; #define MAX(a,b) ((a) > (b) ? (a) : (b)) is function-like. #undef removes a macro definition. Substitution is purely textual and happens before type checking.
Conditional compilation
#if, #ifdef, #ifndef, #elif, #else and #endif select code at build time. The standard include guard is:
#ifndef POINT_H
#define POINT_H
/* declarations */
#endif
Predefined macros include __FILE__, __LINE__, __DATE__, __TIME__ and __STDC__.
Standard library overview
<stdio.h> for I/O; <stdlib.h> for malloc, free, atoi, exit and rand; <string.h> for string functions; <math.h> for sqrt, pow and sin; <ctype.h> for isdigit and toupper; <limits.h> for INT_MAX; plus <time.h> and <stdarg.h>.
Worked example
#define SQUARE(x) ((x) * (x))
SQUARE(2 + 3) /* ((2 + 3) * (2 + 3)) = 25 */
Without the inner parentheses, #define SQUARE(x) x * x would expand the same call to 2 + 3 * 2 + 3, giving 11. Macros also re-evaluate their arguments, so SQUARE(i++) increments i twice.
Traps
#define MAX 100;injects a semicolon into every use.- A space between a function-like macro's name and
(changes its meaning. - Macros have no type checking; prefer
constor an inline function when possible. - A header should hold declarations, not definitions of variables.