0% found this document useful (0 votes)
6 views21 pages

Preprocessors

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 21

C++ Preprocessors

C++ Preprocessors

• As the name suggests Preprocessors are


programs that process our source code before
compilation.
• There are a number of steps involved between
writing a program and executing a program in
C++.
• There are 4 main types of preprocessor
directives:
• Macros
• File Inclusion
• Conditional Compilation
• Other directives
What is Macros?

• Macros: Macros are a piece of code in a program which is


given some name. Whenever this name is encountered by the
compiler the compiler replaces the name with the actual
piece of code. The ‘#define’ directive is used to define a
macro.
• #define MAX 5
Example:
#include <iostream>

// macro definition
#define LIMIT 5
int main()
{
for (int i = 0; i < LIMIT; i++) {// LIMIT =5
std :: cout << i << "\n";
}
return 0;
}
The word ‘LIMIT’ in the macro definition is called a macro template and ‘5’ is
macro expansion.
Note: There is no semi-colon(‘;’) at the end of macro definition. Macro
definitions do not need a semi-colon to end.
Macros with arguments
• We can also pass arguments to macros. Macros defined with arguments works
similarly as functions. Let us understand this with a program:
#include <iostream>
// macro with parameter
#define AREA(l, b) (l + b) //LIMIT =5 AREA(10,4)
int main()
{
int l1 = 10, l2 = 5, area;

area = AREA(l1, l2);


std::cout << "Area of rectangle is: " << area;

return 0;
}
File Inclusion

• This type of preprocessor directive tells the compiler to


include a file in the source code program. There are two
types of files which can be included by the user in the
program.
• 1. Header File or Standard files.
• 2. user defined files.
Header File or Standard files
• These files contains definition of pre-defined functions like printf(),
scanf() etc. These files must be included for working with these
functions. Different function are declared in different header files.
For example standard I/O functions are in ‘iostream’ file whereas
functions which perform string operations are in ‘string’file.
Syntax:
• #include< file_name >
• Example:
• #include<string>
user defined files

• When a program becomes very large, it is good


practice to divide it into smaller files and include
whenever needed. These types of files are user
defined files.
• Syntax:

#include"filename“
#include”stringPro.cpp”
Conditional Compilation

• Conditional Compilation directives are type of


directives which helps to compile a specific portion
of the program or to skip compilation of some
specific part of the program based on some
conditions
• This can be done with the help of two preprocessing
commands ‘ifdef‘ and ‘endif‘.
Syntax:

#ifdef
macro_name
statement1;
statement2;
statement3; . . .
statementN;
#endif
2. Array is used for storing _________

A. Similar type of elements C. Different data


type of elements

B. Store in same memory location D. All of the


mentioned
MCQ

Find out the error


#include<iostream>
int main()
{
std::cout<<”hello world”;
cout<<” welcome”;
return 0;
}
3. What is the output of the following
#include <iostream>
using namespace std;
main()
{
Int a[]={1,2},*p=a;
Cout<<p[1];
}
A. 1 C. 2

B. Compile time error D. Run time error


4. What will be the output of the following code?
class Abc
{
int a=23;
float f=30;
void display()
{
cout<< “value of a==”<<a<<endl;
cout<<”value of f==”<<f;
}};
Int main()
{ Abc obj;
obj.display();
}
A. 23 C. Logical error
30
B. Private data member are not initialize in directly D. None
5. Predict the output
#include<iostream>
#include<string>
using namespace std;
int main()
{
string x("My ");
char y[]= "school";
string s= x+y;
cout<<"s="<<s<<endl;

A. s=My school C. Will generate error


at
cout<<"s="<<s<<en
dl; statement
B. Will generate error at string s= x+y; statement D. Will generate error
at string x("My ");
statement
6. Which is incorrect about 2D array
A. int arr[2][2]={1,2,3,4}; C. int arr[]
[2]={1,2,3,4};

B. int arr[2][]={1,2,3,4}; D. All are correct


initializations of
2D Array
7. Constructor can be called
A. By compiler C. By user
B. Both D. None

8. Is it possible to declare inline function as a static .

A. Not possible C. Yes It is

B. Depends on D. Depends on compiler


programmer
9. In CPP, cin and cout are the predefined stream __________ .

• A. Operator
• b. Functions
• c. Objects
• d. Data types
10. When a class is defined inside any function or block, it is
called ___________ .
11. Logical expressions produce ____________ type results.
12. Static variable declared in a class are also called_________ .
12. If a class contains static variable, then every object of the
class has its copy of static variable.
a. True
b. False
13. __________________ is the OOP feature and mechanism
that binds together code and the data it manipulates, and
keep both safe from outside world.
14. ____________ refers to the act of representing only
essential features without including the background details.
15. Static variable must be declared in public section of the class.
Thank you

You might also like