Unit-5(Part-4)(Preprocessor Directive and Command Line Arguments)
Unit-5(Part-4)(Preprocessor Directive and Command Line Arguments)
Preprocessors
Preprocessors are programs that process our source code before
compilation. All of these preprocessor directives begin with a „#‟ (hash)
symbol. This („#‟) symbol at the beginning of a statement in a C/C++
program indicates that it is a pre-processor directive. We can place these
preprocessor directives anywhere in our program. Examples of some
preprocessor directives are: #include, #define etc.
1. 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.
There are two types of macros: Object-like (do not take parameters) and
function-like (Can take parameters)
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)
i) object-like macro
#define p 10
#define PI 3.14
Ex:1)
#include<stdio.h>
// macro definition
#define PI 3.14
void main()
{
int r=2;
float area;
area=PI*r*r;
printf("Area of circle=%f”,area);
}
}
Output:
Area of circle=12.56
Ex:2)
#include <stdio.h>
// macro definition
#define LIMIT 5
void main()
{
for (int i = 0; i < LIMIT; i++) {
printf("%d \n",i);
}
}
Output:
0
1
2
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)
2
3
4
In the above program, when the compiler executes the word LIMIT it
replaces it with 5. 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.
For example,
Ex)
#include <stdio.h>
#define AREA(l, b) (l * b)
void main()
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)
Output:
We can see from the above program that whenever the compiler finds
AREA(l, b) in the program it replaces it with the statement (l*b) . Not only
this, the values passed to the macro template AREA(l, b) will also be
replaced in the statement (l*b). Therefore AREA(10, 5) will be equal to
10*5.
2) File Inclusion:
This type of preprocessor directive tells the compiler to include a file in the source code program.
Including all the files from library that our program needs. we write
#include which is a directive for the preprocessor that tells it to include
the contents of the library file specified. For example, #include will tell
the preprocessor to include all the contents in the library file stdio.h.
i) #include <stdio.h>
Note:1) These directives tell the CPP to get stdio.h from System Libraries
and add the text to the current source file. The next line tells CPP to get
myheader.h from the local directory and add the content to the current
source file.
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)
enclosed within double quotes, the search path is expanded to include the
current source directory.
Ex)
#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
int i;
5
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)
for(i=0;i<argc;i++)
printf("\n%s",argv[i]);
getch();
}
Output:
c:\turboc3\source>com aman raj rahul akshat (or
c:\turboc3\source>com.exe aman raj rahul akshat)
c:\turboc3\source>com.exe
aman
raj
rahul
akshat
Ex:2)
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main(int argc,char *argv[])
{
char ch;
FILE *fp;
int c=0;
clrscr();
fp=fopen("com.cpp","r");
if(fp==NULL)
{
printf("\nFile not found");
getch();
exit(0);
}
printf("\n Contents of the file::\n");
6
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)
while((ch=fgetc(fp))!=EOF)
{
c++;
printf("%c",ch);
}
fclose(fp);
printf("\n The number of characters present in file is=%d",c);
getch();
}//end of main().
Output:
c:\turboc3\source>co (or
c:\turboc3\source>co.exe)
Ex:3)
#include <stdio.h>
#include <conio.h>
void main(int argc, char *argv[])
{
int i;
if( argc >= 2 )
{
printf("The arguments supplied are:\n");
for(i = 1; i < argc; i++)
{
printf("%s\t", argv[i]);
}
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)
}
else
{
printf("argument list is empty.\n");
}
getch();
}
Note:1) It should be noted that argv[0] holds the name of the program
itself and argv[1] is a pointer to the first command line argument supplied,
and *argv[n] is the last argument. If no arguments are supplied, argc will
be one, and if you pass one argument then argc is set at 2.
Note:2) You pass all the command line arguments separated by a space,
but if argument itself has a space then you can pass such arguments by
putting them inside double quotes "" or single quotes ''. Let us re-write
above example once again where we will print program name and we also
pass a command line argument by putting inside double quotes –
//file name test.cpp
#include <stdio.h>
if( argc == 2 ) {
else {
}
8
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)
Output:
@Jeetesh Srivastava(UCEM-CSE)