3.7 Command-Line Arguments
3.7 Command-Line Arguments
1 Department of CSE
Objectives
2 Department of CSE
Agenda
• Main Function
• Command-Line arguments
Types of arguments
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.
• 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()
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
9 Department of CSE
Rules to be followed
• 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?
/* 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)?
/* 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.
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