¤ 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;



Share:




comments powered by Disqus


Download Mobile App for Android


Web Service Offers

.CLUB Registration
1,199 199 (83.4% off)
per Year

Ecommerce on the Cloud

An online store hosted on the Cloud. Sell unlimited products. Integrated business functions. Well-suited for high traffic website.

39,999 29,999 (25% off)

Ecommerce Premium

An e-commerce website well-suited for companies having a large portfolio of products. Sell unlimited products. Manage both offline and online sales from one system.

40,000 25,000 (37.5% off)

Ecommerce Business

Launch your online business. Sell up to 5000 products. Collect payment via multiple online and offline payment methods.

35,000 25,000 (28.57% off)

Ecommerce Pro

Get a Mobile friendly online store front. Sell up to 1000 products.

35,000 25,000 (28.57% off)

Ecommerce Starter

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

35,000 25,000 (28.57% off)

Website on the Cloud

Custom built, responsive corporate website hosted on the cloud. Mirrored on 3 servers. Includes visitor signup with CRM functions/

19,999 9,999 (50% off)

Website Corporate

Custom built, responsive corporate website with CRM. Our most popular website plan.

19,000 9,000 (52.63% off)

Website Business - 1000 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

18,000 8,000 (55.56% off)

Website Business - 500 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,500 7,500 (57.14% off)

Website Business - 100 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,000 7,000 (58.82% off)

Website Business - 50 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,500 6,500 (60.61% off)

Website Business - 25 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,000 6,000 (62.5% off)

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% 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