¤ Home » Programming » C Tutorial » Preprocessor Directive in C

Preprocessor Directive in C

In the previous posts, in all C programs you would have noticed that the first line is written as #include <stdio.h>. In programs where we used mathematical calculations, we had two such include directives, as below:

#include <stdio.h>
#include <math.h>

Although those two lines look like program statements, they are not part of a function body and do not end with a semicolon, as program statements must. Instead, they start with a hash sign (#), and are called preprocessor directives. While program statements are instructions to the computer, a preprocessor directive, is an instruction to the compiler itself. A part of the compiler, called the preprocessor, deals with these directives before it begins the real compilation process.

The preprocessor directive #include tells the compiler to insert another text file (known as header file) into your C source code file. In fact, the #include directive is replaced by the contents of the file indicated, before compilation begins. The filenames stdio.h and math.h are examples of header files. These header files contain definitions of standard C utility programs and without these declarations the compiler will not allow the usage of functions that are defined in these files. For instance, commonly used standard input and output functions such as getchar, putchar, scanf and printf, etc. are defined in the stdio.h file. Thus reference to any such function inside the program requires the corresponding header file to be included using #include preprocessor directive. The symbols < and >, that enclose the header filename indicates that the header file is located under the default include directory of the C compiler.

Thus, you can build your own library of functions that are put in files with .h extension and place them in the default include directory of your C compiler, and thereby enhance the functionality of C. Alternatively, you can place such include files in the same directory as your C program and include the file as below:

#include "myfunction.h"

By using quotes instead of angle-brackets, you are telling the preprocessor to first look for the file in the same directory as your C program. If the file is not found there, the preprocessor will look for it in the default include directory.

Another type of preprocessor that we have seen being used in previous posts, is #define. This preprocessor directive can be used to define constants in C. For example, when you add a define directive in your program as below:

#define PI 3.14

Before the compilation begins, all occurrences of PI occurring in the C program, after the above directive, are replaced with the value 3.14.

You can even use #define to define a function (referred as macros). See an example below:

#define getmax(a,b) a>b?a:b

With the above define directive in place, you can have a C statement as:

int a=5, b=6;
max = getmax(a,b);

In the above, wherever getmax(a,b) is used, the preprocessor will replace it with a>b?a:b. So the second statement above will become as below, before compilation:

int a=5, b=6;
max = a>b?a:b;


comments powered by Disqus

Download Mobile App for Android

Web Service Offers

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting included.

About the Author

Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.

Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy