Preprocessor
Preprocessor
4 Types of Preprocessor Directives
#define SIZE 5 // All occurrences of `SIZE` will be replaced by `5`. int array[SIZE]; for (int i = 0; i < SIZE; i++) { scanf(" %d", &array[i]); } for (int i = 0; i < SIZE; i++) { printf("%d \n", array[i]); }#include <stdio.h> #define PI 3.14159265 #define CIRCLE_AREA(r) (r*r*PI) int main() { float radius = 10.0, area; area = CIRCLE_AREA(radius); printf("Area of the circle : %ld", area); return 0; }#ifdef macro_name // code to be compiled if a macro named // `macro_name` is defined. // if it's not defined, compiler will just // skip this chunk of code. #endif#if defined macro_name // code to be compiled if a macro named // `macro_name` is defined. #elif defined another_macro // code to be compiled if the macro // above is not defined. // and a macro named `another_macro` is defined. #else // code to be compiled if above two macros // are not defined. #endif
Last updated