Monday, June 9, 2008

What are preprocessor directives in C++?

C++ supports the following preprocessor directives.

#define directive

The #define directive replaces all instances of the identifier with the defined string. Identifiers used in comments and part of strings are ignored.

To write the #define directive in multiple lines backslash(\) could be used.

In C++ it is better to use "const = " instead of #define for constants in C++ and use inline template functions instead of function macros.

 #define directive can be used to create compile time flags for conditional inclusion of code. (e.g. DEBUG)

Example: Demonstrate the usage of #define

#include <iostream>
using namespace std;

// Avoid. Using #define for constants
#define MY_CONST1 1000

// Use. Equivalent constant definition
const int MY_CONST2 = 2000;

// Avoid. Using #define for function like macros
#define SQR1(x) (x*x)

// Use. Equivalent funtion definition
inline template <typename T>
T SQR2 ( T a ) {
return a*a;
}

// Writing #define in multiple lines
#define MAX(a,b) \
((a) > (b) ? (a) : (b))

// Compile time flags
#define DEBUG

int main() {
cout << "SQR1 = " << SQR1(10) << endl;
cout << "SQR2 = " << SQR2(10) << endl;
cout << "MAX = " << MAX(10,11) << endl;
cout << "MY_CONST1 = " << MY_CONST1 << endl;
cout << "MY_CONST2 = " << MY_CONST2 << endl;
return 0;
}
Output:-
SQR1 = 100
SQR2 = 100
MAX = 11
MY_CONST1 = 1000
MY_CONST2 = 2000

Predefined Macros

Few macros are predefined in C++.
__cplusplus - If defined the compiler used is C++.
__STDC__ - If defined the compiler used is Standard C.
__DATE__ - Expands to the data of compilation.
__TIME__ - Expands to the time of compilation.
__FILE__ - Expands to the name of source file.
__LINE__ - Expands to the line number in the source file.

Example: Demonstrate the usage of predefined macros

#include <iostream>
using namespace std;

int main()
{
#ifdef __cplusplus
cout << "C++" << endl;
#endif

#ifdef __STDC__
cout << "C" << endl;
#endif

cout << __DATE__ << endl;
cout << __TIME__ << endl;
cout << __FILE__ << endl;
cout << __LINE__ << endl;

return 0;
}
OUTPUT:-
C++
Jun 10 2008
20:10:34
predefined.cpp
26

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

Directives #if, #ifdef, #ifndef, #elif, #else, #endif are used to mark regions for conditional compilation.
#if, #ifdef, #ifndef being a region of conditional compilation.

#elif, #else are always associated with one of #if, #ifdef, #ifndef and are optional in the conditional compilation block.

#endif is used to end a region of conditional compilations.

Other main use of these directives is for header file guards against multiple includes in source files. Example:-
#ifndef MYHEADER_H
#define MYHEADER_H

// Header contents go here

#endif // MYHEADER_H
EXAMPLE: Demonstrate conditional compilation directives
#include <iostream>
using namespace std;

#define VAR1
#define VAR2

int main()
{
#if defined(VAR0)
cout << "VAR0 is defined" << endl;
#elif defined(VAR1)
cout << "VAR1 is defined" << endl;
#else
cout << "Nothing is defined" << endl;
#endif

#ifdef VAR1
cout << "VAR1 is defined" << endl;
#endif

#ifndef VAR0
cout << "VAR0 is not defined" << endl;
#endif

return 0;
}
Output:-
VAR1 is defined
VAR1 is defined
VAR0 is not defined

#undef directive

#undef directive is used to delete the definition of a macro.

EXAMPLE: Demonstrate usage of #undef directive
#include <iostream>
using namespace std;

#define HELLO

int main()
{
#ifdef HELLO
 cout << "HELLO is defined" << endl;
#else
 cout << "HELLO is NOT defined" << endl;
#endif

#undef HELLO

#ifdef HELLO
 cout << "HELLO is defined" << endl;
#else
 cout << "HELLO is NOT defined" << endl;
#endif
}
Output:-
HELLO is defined
HELLO is NOT defined

#pragma directive

This directive offers compilers to offer machine and operating system specific functionality.

EXAMPLE: Demonstrate usage of #pragma
#include <iostream>
using namespace std;

int main()
{
#ifdef __BORLANDC__
   #pragma message("Borland C++ Compiler in Use")
#endif
}
Output:-
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
pragma.cpp:
Borland C++ Compiler in Use
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

#line directive

#line directive changes the default values for __LINE__ and __FILE__.
Useful in code generators and debugger implementations.

EXAMPLE: Demonstrate the usage of #line directive
#include <iostream>
using namespace std;

int main()
{
 cout << __FILE__ << endl;
 cout << __LINE__ << endl;

 #line 100 "new_file.cpp"

 cout << __FILE__ << endl;
 cout << __LINE__ << endl;

 return 0;
}
OUTPUT:-
line.cpp
7
new_file.cpp
102

#error directive

#error directive informs the preprocessor to report as if a programming error has occurred at compile time.

EXAMPLE: Demonstrate the usage of #error directive
#include <iostream>
using namespace std;

#define VAR

int main()
{
   #ifndef VAR
   #error VAR not defined
   #endif

   #undef VAR

   #ifndef VAR
   #error VAR not defined
   #endif

   return 0;
}
Output:-
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
error.cpp:
Fatal F1003 error.cpp 18: Error directive: VAR not defined in function main()
*** 1 errors in Compile ***

#include directive

Includes the contents of a standard header or some source file.

Replaces the directive with the entire contents of the header or source file.

The include directive in quote form (#include "myheader.h") searches in the current directory or in the directory that contains the source file.

The Angle bracket form (#include ) searches only in standard system directories.

Contact Form

Name

Email *

Message *

Back to Top