C++ #line, #error , #undef , #pragma preprocessor directives

There are some other directives like #line, #error , #undef and #pragma that might be useful in some ways and may not in other ways.They are explained below.

#line

The #line directives is used for changing the number of the line.This directive does not require a macro but instead it accept a literal(a value).Suppose the value associated with the directive is 90-“#line 90“,then the line next to the directive will have a line number 91.Consider the program below.

int main( )
{
#line 90
 cout<< __LINE__ ; //this macros gives the line number

cin.get();
return 0 ;
}

Run the program and see if the output of the line number match with the value shown in the left margin of the line number column in your IDE editor or does it give 91.


#error

#error directives is used to give an error message when the compiler encounters an error.This directive is mainly used for debugging purpose.


#undef

The function of #defined directive is to bring an existence of a new macro.The #undef does the opposite of #define.So #undef takes away the existence of a macros.


Link: C++ #define directive

#define __RISE
int main( )
{
#undef __RISE

#if defined(__RISE)
  cout<< “__RISE is defined \n” ;
#else
  cout<< “__RISE is not defined \n” ;
#eindif

cin.get( ) ;
return 0 ;
} 

You can run the program to witness the function of #undef directives.


#pragma

#pragma is implemetation-defined directive through which we can pass instructions that are meant for the compiler.One use of #pragma that you will come across if you open the _mingw.h(present in code::blocks IDE) header file you will find.

#pragma GCC system_header

The directive #pragma tell the compiler that the included file starting from the next line to ‘#pragma GCC system_header’ to be considered as system header.What is system header? that you might wanna check up in the Google.

There are around 4 post including this which describes all the directives found in C++.I am sure nothing is left out if I did comment below and let me know.And the macro which is part of the #define directive is discussed in another post so do check it out.



Leave a Reply

Your email address will not be published. Required fields are marked *