Preprocessor
/*
* Preprocessor directives
* Copyright (c) 2003 Chris Provenzano
*/
/* Comments are stripped by the preprocessor */
#include <stdio.h>
#define PI 3.14159
int main() {
double radius = 2.0;
#if defined(PI)
printf("We have PI defined as %lf\n", PI);
#endif
#undef PI
#ifdef PI
printf("We never print this line.\n");
#else
printf("PI is no longer defined.\n");
#endif
#if 0
printf("This line is never printed\n");
#endif
#ifndef PI // Same as #if !defined(PI)
#define PI 3.1415927 // More precision
#endif
#define AREA(x) \
(x) * (x) * PI
printf("Area of circle radius %lf is %lf\n", radius, AREA(radius));
printf("The incorrect area of circle radius %lf is %lf\n",
radius, AREA(++radius));
return(0);
}
next slide