C++ - SPLessons

C++ Preprocessor

Chapter 10

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

#include <iostream>
using namespace std;

#define length 20

int main ()
{
 
    cout << "Value of length is " << length << endl;
    return 0;
}

Output:

Value of length is 20

Example program using macros to call functions

#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;
}

Output

The maximum of given numbers is 11
else is included in the source code

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

#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;
}

Output:

else is included in the source code

Example for ifdef,else,endif

#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;
}

Output:

splessons is defined.It can be added in the C++ file

undef is used to un-define the existing macro variable.

//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;
}

Output

First defined value for height 20
length after undef 20

# and ## symbols in C++

shape Description

# symbol in C++ are used to replace the inserted text with the strings.

//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;
}

Output

Welcome to splessons site

## symbol is used for concatenation of arguments.

#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;
}

Output

The value after concatenation is 15

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).