0% found this document useful (0 votes)
42 views

Lecture 2 C++ Preprocessor Directives

Uploaded by

Dihahs Kilam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Lecture 2 C++ Preprocessor Directives

Uploaded by

Dihahs Kilam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

C++ Preprocessor Directives

Dr. Rahat H. Bokhari


Outline

 What is a preprocessor directive?


 Why do we use it?
 How is it executed?
 What are its benefits?
 Summary
STRUCTURE OF C++ PROGRAM

 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

 __TIME__ The time of translation of the preprocessed translation unit.


 __DATE__ The compilation date of the current source file. ...
 __FILE__ The name of the current source file. ...
 __LINE__ Defined as the integer line number in the current source file
 Etc.
Macro
#define Directives

#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

 Header File or Standard files


 #include using < >: While importing file using angular brackets (< >),
the preprocessor uses a predetermined directory path to access the file. It
is mainly used to access system header files located in the standard
system directories.
 #include <Header-file>

 #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

 The following directives allow you to conditionally include portions of code


based upon the outcome of a constant expression.
#if, #else, #elif, and #endif
 The most commonly used conditional compilation directives are the #if,
#else, #elif, and #endif.
 The general form of #if is
#if constant-expression
statement sequence
#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

 #pragma startup and #pragma exit.


 Such directives help us to specify the functions that are needed to
run before program startup (before the control passes to main(),
and just before program exit (just before the control returns from
main().
Preprocessor Directive

/C++
Preprocessor Directives

 Summary

#define #elif #else #endif


#error #if #ifdef #ifndef
#include #line #pragma #undef
References

 Farrell, J. (2009) Object-Oriented Programming Using C++, 4th Edition,


Course Technology, USA.
 Trivedi, B. (2012) Programming With Ansi C++, 2nd Edition, Oxford
University Press, India
 Herbert Schildt, H. (2003) C++: The Complete Reference,4th Ed.,
McGraw-Hill, USA.
 https://www.geeksforgeeks.org/cc-preprocessors/

You might also like