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

3.7 Command-Line Arguments

The document discusses command line arguments in C programs. It explains that command line arguments allow programs to receive input when executed rather than having input hardcoded. The main function can receive two parameters - argc for the number of arguments and argv which is an array of strings containing the arguments. Command line arguments make programs more dynamic by allowing different inputs with each run. Examples are provided to demonstrate using command line arguments to pass filenames, numbers, and other inputs to programs.

Uploaded by

Keerthi Vasan S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

3.7 Command-Line Arguments

The document discusses command line arguments in C programs. It explains that command line arguments allow programs to receive input when executed rather than having input hardcoded. The main function can receive two parameters - argc for the number of arguments and argv which is an array of strings containing the arguments. Command line arguments make programs more dynamic by allowing different inputs with each run. Examples are provided to demonstrate using command line arguments to pass filenames, numbers, and other inputs to programs.

Uploaded by

Keerthi Vasan S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

3.

7 Command Line Arguments

1 Department of CSE
Objectives

• To understand how arguments can be passed to main function.


• To give input to the program when it is executed.
• To make program execution dynamic by changing input for each run.

2 Department of CSE
Agenda
• Main Function
• Command-Line arguments
 Types of arguments

• Example Programs using command-Line arguments


• Exercises

3 Department of CSE
Main Function
• All C language programs must have a main() function.
• It's the core of every program.
• It contains instructions that tell the computer to carry out whatever task your
program is designed to do.
• The main function can also have arguments

4 Department of CSE
Command-Line arguments
• Arguments to the main function is called Command-Line arguments.

• A command-line argument is the information that follows the name of


the program on the command line of the operating system.

• Command-line arguments are used to pass information into a program


when the program is executed.

• Eg: When we write program to append two files ,the file names are
supplied when program starts executing rather than specifying it as
constants.

5 Department of CSE
Introduction-Continued…
• C defines two built-in parameters to main()

 The parameters receive the command line arguments


 Their names are argc and argv

Note: The names of the parameters are arbitrary. However, argc


and argv have been used by convention for several years.

6 Department of CSE
Types of Parameters
• int main( int argc , char *argv[] )

argc:
 Holds the number of arguments on the command line
 Since the name of the program always corresponds to the first
argument, it is always at least 1
 argc is an integer
 The value for this argument is not entered by the user.
 The system determined it from arguments that user specifies when
program is executed.

7 Department of CSE
Types of parameters-continued…
int main(int argc, char *argv[] )
argv[]
 Argv is a pointer to an array of character pointers.
 Each character pointer in the argv array corresponds a string
containing a command-line argument
Eg: argv[0] points the name of the program, argv[1] points to
the first argument, argv[2] points to the second argument, ...
 Each command-line argument is a string
 If you want to pass numerical information to your program, your program
should convert the corresponding argument into its numerical equivalent.
 Each command-line argument must be separated by spaces or tabs

8 Department of CSE
Syntax

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


{
}
When the program is executed:
In command prompt: $./a.out string1 string2…. stringN

9 Department of CSE
Rules to be followed

• All command-line arguments are passed to the program as strings


 program should convert them into their proper internal
format.

• As a programmer, the names of the parameters in main can be specified, but the
types and format are predefined for the language.

10 Department of CSE
Illustrations
/*Program to print command-Line arguments*/ /*Program to add two numbers*/
#include<stdio.h> #include<stdio.h>
int main(int argc,char * argv[]) void main(int argc, char * argv[]) {
{ int i, sum = 0;
int i;
printf(“Number of arguments is:%d\n",argc); if (argc != 3) {
printf(“Name of the program is :%s\n",argv[0]); printf("You have forgot to type numbers.");
for(i=1;i<argc;i++) exit(1);
{ }
printf(“User entered string value no %d is %s\n",i,argv[i]); printf("The sum is : ");
} for (i = 1; i < argc; i++)
} sum = sum + atoi(argv[i]);
printf("%d", sum);}
Output:
[d_bharathi@ssh ~]$ cc command1.c Output:
[d_bharathi@ssh ~]$ ./a.out welcome [d_bharathi@ssh ~]$ cc addcommand.c
No of arguments is :2 [d_bharathi@ssh ~]$ ./a.out 5 7
Name of the program is:./a.out The sum is : 12
User entered string value no 1 is welcome
11 Department of CSE
Illustration with files-Program to copy one
file content to another file
/* File Copy using Command line arguments */ ft=fopen(argv[2],"w");
if(ft==NULL)
#include<stdio.h> {
int main(int argc,char *argv[]) printf("Can't open target file.");
{ fclose(fs);
FILE *fs,*ft; return 1;
int ch; }
if(argc!=3)
{ while(1)
printf("Invalide numbers of arguments."); {
return 1; ch=fgetc(fs);
} if (feof(fs)) break;
fs=fopen(argv[1],"r"); fputc(ch,ft);
if(fs==NULL) }
{
printf("Can't find the source file."); fclose(fs);
return 1; fclose(ft);
} return 0;
}

12 Department of CSE
Illustration with files-Program to copy one file content to another
file-continued…
Output:
[d_bharathi@ssh ~]$ vi con.c
[d_bharathi@ssh ~]$ vi sample.txt
[d_bharathi@ssh ~]$ vi result.txt
[d_bharathi@ssh ~]$ cc con.c
[d_bharathi@ssh ~]$ ./a.out sample.txt result.txt
[d_bharathi@ssh ~]$ vi result.txt

13 Department of CSE
Finding the output
1. What will be the output of the program (myprog.c) given below if it is executed
from the command line?

cmd> myprog one two three

/* myprog.c */
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{ printf("%s\n", argv[1]);
return 0; }

Output:
one
14 Department of CSE
Finding the output-continued…
2. What will be the output of the program (sample.c) given below if it is executed
from the command line (turbo c under DOS)?

cmd> sample Good Morning

/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{ printf("%d %s", argc, argv[1]); return 0; }

Output: 3 Good

15 Department of CSE
Debugging code
1. What will be the output of the program (sample.c) given below if it is executed from the
command line ?

cmd> sample 1 2 3

/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{ int j; j = argv[1] + argv[2] + argv[3];
printf("%d", j); return 0; }

Output:Error
Explanation: Here argv[1], argv[2] and argv[3] are string type. We have to convert the string
to integer type before perform arithmetic operation.
Example: j = atoi(argv[1]) + atoi(argv[2]) + atoi(argv[3]);

16 Department of CSE
Simple word problems
1. Every time we supply new set of values to the program at command prompt, we
need to recompile the program.
Answer: No only input will be changed.

2. The first argument to be supplied at command-line must always be count of total


arguments
Answer: No, The system determined it from arguments that user specifies when
program is executed.

17 Department of CSE
Summary
• The Command-Line arguments provides input to the program
during run time.
• Command-Line arguments are optional.

18 Department of CSE

You might also like