Preprocessor Directives

October 24, 2020

Preprocessor Directives

SL.No. Directive Description
1 #define Substitutes a preprocessor macro
2 #include Inserts a particular header from another file
3 #undef Undefines a preprocessor macro
4 #ifdef Returns true if this macro is defined
5 #ifndef Returns true if this macro is not defined
6 #if Tests if a compile time condition is true
7 #else The alternative for #if
8 #elif #else and #if in one statement
9 #endif Ends preprocessor conditional
10 #error Prints error message on stderr
11 #pragma Issues special commands to the compiler, using a standardized method

The #define Directive

/* Fahrenheit to Celcius conversion table*/
#include<stdio.h>
#define LOWER 0
/* lower limit of temp. table (in Fahrenheit) */
#define UPPER 300
/* upper limit */
#define STEP 20
/* step size */
int main(void){
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr += STEP)
printf("%3d \t%6.1f\n", fahr, (5.0/9.0) * (fahr−32.0));
}
//Area of circle using #define preprocessor
#include <stdio.h>
#define PI 3.14
#define AreaofCircle(r) (PI*r*r)

int main() {
    float radius, area;
    printf("Enter the radius: ");
    scanf("%f", &radius);
    area = AreaofCircle(radius);
    printf("Area = %.2f", area);
    return 0;
}

Predefined Macros

SL.No. Macro Description
1 __DATE__ The current date as a string literal in “MM DD YYYY” format.
2 __TIME__ The current time as a string literal in “HH:MM:SS” format.
3 __FILE__ This contains the current filename as a string literal.
4 __LINE__ This contains the current line number as a decimal constant.
5 __STDC__ Defined as 1 when the complier with the ANSI standard.

Example:

#include<stdio.h>
void main()
{
  printf("Date: %s\n",__DATE__);
  printf("Time: %s\n",__TIME__);
}

Output:

Date: Oct 24 2020
Time: 22:40:32

Written by Anushka Raj who likes to teach programming and work on front-end technologies. Follow me on Twitter and Instagram.