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

Unit-5(Part-4)(Preprocessor Directive and Command Line Arguments)

Uploaded by

rameshjaiswal365
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)
49 views

Unit-5(Part-4)(Preprocessor Directive and Command Line Arguments)

Uploaded by

rameshjaiswal365
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/ 9

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.

There are a number of steps involved between writing a program and


executing a program in C / C++. Let us have a look at these steps

There are 2 main types of preprocessor directives:


1. Macros
2. File Inclusion

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

The „#define‟ directive is used to define a macro.

#define p 10

#define PI 3.14

#define NAME “Raj”

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.

ii) Function like Macro:

 We can also pass arguments to macros. Macros defined with


arguments works similarly as functions. This is known as function-like
macros.

 If we have to write a function (having a small definition) that needs to


be called recursively (again and again), then we should prefer a macro
over a function.

For example,

#define sum(a,b) (a+b)

#define circleArea(r) (3.14*(r)*(r))

Every time the program encounters circleArea(argument), it is replaced by


(3.1415*(argument)*(argument)).

Suppose, we passed 5 as an argument then, it expands as below:

circleArea(5) expands to (3.14*5*5)

Ex)

#include <stdio.h>

// macro with parameter

#define AREA(l, b) (l * b)

void main()

@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)

int l = 10, b = 5, area;

area = AREA(l, b);

printf("Area of rectangle is: %d", area);

Output:

Area of rectangle is: 50

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

 There exist 2 ways to write #include statement. These are

i) #include <stdio.h>

ii) #include "myheader.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.

Note:2) If the filename is enclosed within angle brackets, the file is


searched for in the standard compiler include paths. If the filename is

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

Command Line Arguments


It is possible to pass some values from the command line to your C
programs when they are executed. These values are called command line
arguments and many times they are important for your program especially
when you want to control your program from outside instead of hard coding
those values inside the code.
To pass command line arguments, we typically define main() with two
arguments : first argument is the number of command line arguments and
second is list of command-line arguments.
Syntax:
int main(int argc, char *argv[])
1) argc:
 argc (ARGument Count) is int and stores number of
command-line arguments passed by the user including the
name of the program. So if we pass a value to a program,
value of argc would be 2 (one for argument and one for
program name)
 The value of argc should be non negative.
2) Argv[ ]:
 argv(ARGument Vector) is array of character pointers listing
all the arguments.
 If argc is greater than zero,the array elements from argv[0] to
argv[argc-1] will contain pointers to strings.
 Argv[0] is the name of the program , After that till argv[argc-1]
every element is command -line arguments.

Ex)

//file name com.cpp

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

//file name: co.cpp

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

//file name com.cpp


#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
int i;
for(i=0;i<argc;i++)
printf("\n%s",argv[i]);
getch();
}

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>

int main( int argc, char *argv[] ) {

printf("Program name %s\n", argv[0]);

if( argc == 2 ) {

printf("The argument supplied is %s\n", argv[1]);

else if( argc > 2 ) {

printf("Too many arguments supplied.\n");

else {

printf("One argument expected.\n");

}
8

@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-4)(Preprocessor directive and Command Line Arguments)

Output:

c:\turboc3\source>test "testing1 testing2" (or


c:\turboc3\source>test.exe "testing1 testing2")

Progranm name test

The argument supplied is testing1 testing2

@Jeetesh Srivastava(UCEM-CSE)

You might also like