preprocessors in c++

The preprocessors are the directives, which give instruction to the compiler to preprocess the information before actual compilation starts.

All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not C++ statements, so they do not end in a semicolon (;). We already know #include directive.

Different preprocessor directives (commands) perform different tasks. We can categorize the Preprocessor Directives as follows:
– Inclusion Directives- Macro Definition Directives- Conditional Compilation DirectivesInclusion Directive
This category has only one directive, which is called #include. This inclusion directive is used to include files into the current file. The inclusion directive can be used as follows:
#include <stdio.h>
includes stdio.h from include folder
#include<iostream>
includes cpp class library header iostream
#include<my.cpp>
includes my.cpp file from include folder
#include ”my.h”
includes my.h file from current working folder

Macro Definition Directives
These are used to define macros, which are one, or more program statements (like functions) and they are expanded inline.
There are two directives for Macro Definition:
#define – Used to define a macro
#undef – Used to undefine a macro (The macro cannot be used after it is undefined.)

Conditional Compilation Directives
Conditional Compilation Directives these are used to execute statements conditionally for:
– Executing the code on different machine architectures.
– Debugging purposes.
– Macros can be dependent on other macros (must be defined before they are used.)
– Evaluating codes, which are to be executed depending upon the requirements of the programmer.

The following directives are included in this category:
#if
#elif
#endif
#ifdef
#ifndef


Note: These macros are evaluated on compile time. Therefore they can only use the predefined macros or literals. Most compilers do not support the use of variables with these directives.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s