C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Preprocessor

C++ Preprocessor

Pre-processor:

shape Description

Every C++-program passes through a Pre-compilation process (before the program is compiled) i.e. a process to check the program. CPP Preprocessor performs this process.

shape Conceptual figure

shape Uses

  • Allows the developer to read the program easily
  • Program can be modified without any difficulty
  • CPP Preprocessor makes the program architecture independent
Pre-processor commands are called as Directives. Directives start with # symbol. Basically, there are four CPP Preprocessor Directives in compiler.

File Inclusion Directive

shape Description

The file which consists of "Macro Definitions and C++-function Declarations" is called as Header File.
  • The Header files can be used anywhere in the source files by using Pre-processor directive #include in which "#" symbol implies that the declared statement is Pre-processor pointing to a particular file but not normal C++-language statement.
  • The files declared in Header Files can be used in the main program once declared

Macro: 

shape Description

The #define is a macro, denotes the constant values and they can be of any data types. Eg: #define letter 'A'

shape Example

[c] #include <iostream> using namespace std; #define length 20 int main () { cout << "Value of length is " << length << endl; return 0; }[/c] Output: [c]Value of length is 20[/c] Example program using macros to call functions [c] #include <iostream> using namespace std; #define max(a,b) ((a>b) ? a : b) int main () { int x, y; x= 8; y = 11; cout <<"The maximum of given numbers is " << max(x, y) << endl; #if (a>b) cout << "if is included in the source code" << endl; #else cout << "else is included in the source code " << endl; #endif return 0; }[/c] Output [c]The maximum of given numbers is 11 else is included in the source code[/c]

Conditional Directives:

shape Description

According to the condition, a set of commands can be involved or removed in the program. The conditional directives are used to compile only certain code of the program as per the requirement. For example, if there is #define max(a,b) in the program, it appears as #define max(a>b ? a:b) after the Pre-process commands are compiled.

shape Examples

[c] #include<iostream> #define splessons 10 using namespace std; int main() { #if (splessons==100) cout << "if is included in the source code\n"; #else cout << "else is included in the source code\n"; #endif return 0; }[/c] Output: [c] else is included in the source code [/c] Example for ifdef,else,endif [c] #include<iostream> #define splessons 100 using namespace std; int main() { #ifdef splessons cout << "splessons is defined.It can be added in the C++ file\n"; #else cout << "splessons is not defined\n"; #endif return 0; } [/c] Output: [c]splessons is defined.It can be added in the C++ file[/c] undef is used to un-define the existing macro variable. [c]//Example for undef #include <iostream> using namespace std; #define height 20 int main() { cout << "First defined value for height " << height << endl; #undef length #define length 20 cout << "length after undef " << length << endl; }[/c] Output [c] First defined value for height 20 length after undef 20[/c]

# and ## symbols in C++

shape Description

# symbol in C++ are used to replace the inserted text with the strings. [c]//Example for # operator in C++ #include <iostream> using namespace std; #define splessons(a ) #a int main () { cout << splessons (Welcome to splessons site) << endl; return 0; }[/c] Output [c] Welcome to splessons site[/c] ## symbol is used for concatenation of arguments. [c]#include <iostream> using namespace std; #define concat(i, j) i ## j int main() { int ab = 15; cout << "The value after concatenation is "<< concat(a, b); //Here ## coverts (a,b) to ab return 0; }[/c] Output [c] The value after concatenation is 15[/c]

Summary

shape Key Points

CPP Preprocessor chapter draws out following important points.
  • CPP Preprocessor performs Pre-compilation.
  • Commands used in Pre-processor are called Pre-processor Directives
  • "Macros, Conditional Directives and File Inclusion Directives" are Pre-processor Directives.

shape Programming Tips

Do not get confused between # ( replacing the inserted text with the strings) and ## (concatenation of arguments).