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:
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:
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:
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:
With the above define directive in place, you can have a C statement as:
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:
CEO, Computer Solutions
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.