×

C - PREPROCESSORS


In this chapter we will learn about Preprocessors in C.

What are preprocessors in c?

Preprocessors are a separate step in compilation process. They are included in the program before it is compiled. For example, header files and macros. These instructs the compiler to do required preprocessing before compiling the program.

All the preprocessing starts with a # symbol. For example -

#include<stdio.h>
#define PI 3.14

Below are some of the common preprocessor directives

Sr. No.

Directive

Description

1.

#include

Used to insert header files in the program.

2.

#define

Used to define a preprocessor macro.

3.

#undef

Used to undefine a preprocessor macro.

4.

#ifdef

This is to check whether macro is defined, if defined it will return true.

5.

#ifndef

This is to check whether macro is not defined, if not defined it will return true.

6.

#endif

This indicates the end of conditional statement of preprocessor.


Now Lets uderstand them with some examples:

#include

As we know from above table that this is used to include header file.

Header file:

You have may have seen stdio.h in many c programs, this is a system header file that has some functions defined such as printf() , scanf() and etc.

All the header files have .h file extension.


#include is used in two ways, are :-

#include <header_file>

This is used when we are using system header files.

#include "header_file"

This is used when we are using custom or user defined header files.

File - include_example.c
#include<stdio.h>  //System header file
#include"add.h"   //Custom header file

void main(){
	int a,b;
	a = 55;
	b = 45;
	/*
	* calculating sum of two number
	* using add function
	* defined in custom header file add.h
	*/
	add(a,b);

	/*
	* Printing
	* Sum found :)
	* using printf()
	*/
	printf("Sum found :) \n");
}
File - add.h
void add(int a, int b)
{
    printf("Sum is = %d\n", a + b);
} 

This header file declared is only for learning purpose and idealy its a bad practice to define function definiton in header files.

Output
Sum is = 100
Sum found :)

Note: Here include_example.c and add.h are in same directory.


#define

This is used to define macros in a c program.

Macro

A macro is a name given to a fragment of code. Whenever a macro is used in a program the macro is replaced with its value. For example:

File - macro_define.c
#include<stdio.h>
#define PI 3.141592
void main(){
    printf("The value of pi is %f",PI);
}
Output
The value of pi is 3.141592

#ifdef,#ifndef,#undef and #endif

Below is an example to illustate the use of above preprocessors:

File - using_pi.c
#include<stdio.h>
#define PI 3.141592
#define RADIUS 45
#undef RADIUS //it will undefine RADIUS
void main(){
    #ifdef RADIUS // execute when RADIUS is defined
        printf("Circumference is %f",(PI*RADIUS));
    #endif
    #ifndef RADIUS // execute when RADIUS is not defined
        printf("The value of pi is %f",PI);
    #endif
}
Output
The value of pi is 3.141592

Now if we remove #undef RADIUS from the above example then the output would be:

Output without #undef
Circumference is 141.371640

In the next chapter, we will learn about command line arguments