Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using Preprocessor Directives in C++

Learn more about preprocessor directives in C++ and how they can be implemented.


advertisement

Preprocessors are programs that accept input and produce output that in turn becomes input for the next cycle. Preprocessor directives are statements in a program that are preceded by the hash sign (#). Such directives facilitate portability and conditional compilation of your source code. In this article we will explore the preprocessor directives in C++ with code examples where applicable.

What Are Preprocessor Directives?

When your source code is compiled, the preprocessor statements are evaluated first and if you have specified any header file using preprocessor statements, for example, those header files are included in your code before the compiler can start the process. Preprocessor directives provide support for inclusion of header files, macro expansions, conditional compilation, and line control. Note that the preprocessor directives are specified in a single line of code and they don't need an ending semicolon.

#include

One common use of the preprocessor directive is in the inclusion of a header file. The #include preprocessor directive is used to tell the preprocessor to include the contents of the specified file directly in the source code, at the point where the directive appears. The code example below shows how the stdio.h (standard input output header file) is included in your code.



#include <stdio.h>
int main(void)
{
    printf("Hello, world!\n");
    return 1;
}

#define

This directive is used to specify constants—literals that contain values that cannot change in your source code. Here is an example:

#define MAX_LENGTH 100

The above preprocessor directive specifies a constant named MAX_LENGTH that contains the value 100. Now, you can use this constant to create arrays as shown in the code snippet below.

char arr[MAX_LENGTH];

The above statement defines a string array that can store a maximum of 100 characters.

#undef

This preprocessor directive works exactly the inverse of #define directive. The #undef directive fulfills the inverse functionality of #define. You can use this directive to remove previously defined constants. Here is an example.

#define MAX_LENGTH 100
char firstarray[MAX_LENGTH];
#undef MAX_LENGTH
#define MAX_LENGTH 50
char secondarray[MAX_LENGTH]; 

Note that you can also use the #undef directive to an identifier that has not been defined—this implies that the identifier is undefined.

#ifdef, #ifndef, #if, #endif, #else and #elif

These are also known as conditional directives—you can use them in your code to perform conditional compilation. MSDN states, "Preprocessor directives, such as #define and #ifdef, are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to perform specific actions. For example, the preprocessor can replace tokens in the text, insert the contents of other files into the source file, or suppress compilation of part of the file by removing sections of text."

As an example, the preprocessor directive #ifdef is used to perform conditional compilation if the constant specified is defined. Here is an example:

#ifdef INTEL
//Code that would run only on Intel family of microprocessors.
#endif

Macros

You can also use the #define preprocessor directive to define a macro as shown in the code snippet below:

#include <iostream>
using namespace std;
#define MIN(x,y) (((x)<(y)) ? x : y)
int main ()
{
   int i = 10, j = 20;
   cout <<"The minimum of the two numbers is " << MIN(i, j) << endl;
   return 1;
}

#line

The #line preprocessor directive is used to provide line numbers for compiler messages. This preprocessor directive enables you to modify the compiler's line number for errors and warnings.

#pragma

The #pragma preprocessor directive in C++ is another interesting directive that you can use to access compiler-specific preprocessor extensions. A nice feature of this directive is that you can ensure that a header file is included only once by the compiler, irrespective of the number of times it has been imported.

Suggested Reading

https://gcc.gnu.org/onlinedocs/cpp/

Summary

Preprocessor directives help you to facilitate changing source code seamlessly and also compile the source code in different execution environments easily. In this article we presented a discussion on preprocessor directives in C++ and how they can be implemented.



   

Joydip Kanjilal has more than 16 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date