C-Preprocessor
C-Preprocessor
Computing Lab
https://www.isical.ac.in/~dfslab
Syntax:
#include <stdio.h> /* for include files provided by the system */
#include "common.h" /* for your "own" header files */
Effect ≡ copy-paste (replace the line by the entire contents of the file)
Typically contain #defines, typedefs, variable and function
declarations
Additional directories to search for included files specified as follows
gcc -I/path/to/directory1 -I/path/to/directory2 ...
Syntax
/* From stdio.h */
# define EOF (-1)
Effect ≡ find-and-replace
replace the first string whenever it occurs as a complete word by the
second string
Syntax
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define SQR(x) ((x) * (x))
Effect
replace call to a macro by body of the macro
string-replace each parameter by the corresponding argument
See review question 2.
Syntax
#ifdef DEBUG /* if defined */
fprintf(stderr, "Some error message\n");
#endif
NOTE: All calculations use widest integer type known to the compiler
(typically 64 bits)
Computing Lab (ISI) Preprocessor Directives in C 7/8
Review questions I
int x = 5, y;
y = SQR(x+1);
printf("%d\n", y);