Lecture 2 C++ Preprocessor Directives
Lecture 2 C++ Preprocessor Directives
Include files
Class declaration
Class functions definition
Main function program
C++: Structure of a program
// Comments
/* Comments */
// Preprocessor directives
#include directive
#define directive
All the elements of the standard C++ library are declared within
using namespace std; what is called a namespace, the namespace with the name
class declaration std. So in order to access its functionality, we declare with
class function definition this expression that we will be using these entities.
void main ( )
{
// comments
statements;
Preprocessor Directives
Preprocessor directives tell the compiler to preprocess the source code before
compiling.
Preprocessor directives are not C++ statements, so they do not end in a
semicolon (;).
All the preprocessor directives begin with a ‘#’ (hash) symbol, and only white-
space characters may appear before a preprocessor directive on a line.
Main Types of Preprocessor Directives
Macros
File Inclusion
Conditional Compilation
Other directives
Preprocessor Directives
Macro
The #define preprocessor directive creates symbolic constants.
The symbolic constant is called a macro.
Macros are pieces of code (i.e. Replacement-Text) in a program that is given some
name. Whenever this name is encountered by the compiler, the compiler will replace
all subsequent occurrences of macro in that program by Replacement-Text before the
program is compiled.
The ‘#define’ directive is used for defining a macro.
General form of the directive is:
#define Macro-Name Replacement-Text
#define PI 3.14159
Preprocessor Directives
Macro
Standard Predefined Macro
#include <iostream>
using namespace std;
// macro definition
#define VAL_PI 3.1415
int main ( )
{
cout << "Value of PI is " << VAL_PI << endl;
system ("pause");
return 0;
}
Macro
#define Directives
#include <iostream>
using namespace std;
//object like macro
// macro definition
#define VAL_PI 3.1415
int main( )
{
float r = 2.0;
cout << "Area of circle is "<< VAL_PI * r * r << endl;
system ("pause");
return 0;
Macro
#define Directives
Function-like macros can take arguments, just like true functions
#include <iostream>
using namespace std;
// Function like macro definition
#define mini (X, Y) (X < Y ? X : Y)
int main( )
{
int a, b;
a=23;
b=51;
cout << "Minimum Number between "<<a<<" and "<<b <<" is "<< mini(a ,b) << "\n";
system ("pause");
return 0;
}.
Preprocessor Directives
#include Directive
#include isa way of including a standard/user-defined file in the
program. It is mostly written at the beginning of any C/C++
program.
The process of importing such files either system-defined or user-
defined is known as File Inclusion.
Preprocessor Directives
#include directive
Header File or Standard files
User-Defined files
Preprocessor Directives
#include <iostream>
#include <string.h>
Preprocessor Directives
User-defined files
#include using “ ”: When using the double quotes (“ ”), the
preprocessor access the current directory in which the source
“header_file” is located. This type is mainly used to access any
header files of the user’s program or user-defined files.
#include “User-Defined-file”
Preprocessor Directives
Conditional Compilation
The #define directive is usually coupled with #ifndef and #endif.
The C++ directive #ifndef allows you to test whether a class has
already been defined in a project; if the class has already been
defined, everything up to the #endif directive will be ignored.
#ifndef #ifdef
#define -------
------ #endif
#endif
Preprocessor Directives
Others
#undef Directive
The #undef directive is used to undefine an existing macro.
#undef macro
#error
The #error directive forces the compiler to stop compilation. It is used primarily for
debugging. The general form of the #error directive is
#error error-message
The error-message is not between double quotes
Preprocessor Directives
#pragma Directive
This directive is a special purpose directive and is used to turn on or
off some features. This type of directives are compiler-specific, i.e.,
they vary from compiler to compiler
/C++
Preprocessor Directives
Summary