The parameters passed to the
program when the program is invoked are known as command line arguments. These
parameters are the additional information like filename or other kind of input
to the program. By passing command line arguments there is no need for the user
to provide the input while executing the program.
These command line arguments can
be processed by using the arguments available in the main function.
The main allows two parameters namely: argc and argv. The argc represents
the argument counter which contains the number of arguments passed in the
command line. The argv is a character pointer array which points to
the arguments passed in the command line.
To know the number of command
line arguments, we can use the argc parameter of the main function
and to access the individual arguments, we can use the argv array.
The first element in the argv array is always the program
name. So the first argument can be accessed by using argv[1] and
so on. The prototype of main function will be as shown below:
int main(int
argc,char *argv[])
This
declaration states that
main returns
an integer value ( used to determine if the program terminates successfully)
argc is the
number of command line argument including the command itself.
i.e; argc must be
at least 1.
argv is an
array of command-line arguments.argv[0] contains the program’s name.
Sample Program to add two numbers passed as
command line arguments
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
if(argc!=3)
{printf("Invalid number of arguments...\n");
exit(0);
}
else
{
printf("Sum=%d",atoi(argv[1])+atoi(argv[2]));
}
}
Note:if the default executable is used
then use ./a.out 2 3 for adding two numbers
The function atoi() will convert
string to integer
Program
to display contents of several files
passed as arguments.
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int i=1
int c;
int nargs=0;
FILE *fp;
If(argc==1)
{
printf(“No input file to display);
exit(1);
}
nargs=argc-1;
while(nargs>0)
{
nargs--;
printf(“Displaying the content of file %s”,argv[i]);
p=fopen(argv[i],”r”);
if(fp==NULL)
{
printf(“cannot open file %s”,argv[i]);
continue;
}
c=getc(fp);
while(c!=EOF)
{
putchar(c);
c=getc(fp);
}
fclose(fp);
printf(“\n\n************end of file
%s\n\n”,argv[i]);
i++;
}
return 0;
}
Simulation
of copy command where the two file names are passed as arguments
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
FILE
*in,*out;
int c;
if (argc !=3)
{
printf(“Invalid
number of arguments”);
exit(0);
}
in=fopen(argv[1],”r”);
if(in==NULL)
{
printf(“couldn’t
open the source file..”);
return 0;
}
out=fopen(argv[2],”w”);
if(out==NULL)
{
printf(“couldn’t
open the destination file..”);
return 0;
}
while(!feof(in))
{
c=fgetc(in);
if(!feof(in))
fputc(c,out);
}
fclose(in);
fclose(out);
return 0;
}
Keep sharing such a useful information..!! Great blog. Computer programming courses
ReplyDeleteI have read all your articles. I wonder why you can write them. They are easy to understand and accessible. They help me much too. I'm thankful if you can upload more posts later. Thanks for your sharing.
ReplyDeleteHướng dẫn đăng ký B30 VinaPhone, Đăng ký Mimax sinh viên VinaPhone, Đăng ký Mimax50 VinaPhone, Đăng ký 3G VinaPhone 1 ngày chỉ 5K,7K,10K
recent sales notification popup 2018
ReplyDeletepop sales
Say, you got a nice post.Much thanks again. Really Cool
I found best article.thank you for sharing useful info.
ReplyDeleteDiscount master app
Discount master app
For space outside the milk tea shop closer to nature, outdoor space should use speakers with greater capacity, products such as garden speakers, imitation stone speakers, are products with designs The most natural to create interesting feeling when enjoying. Because it must work in the weather conditions heat so the shell is made very firmly with high durability, the quality of the speaker is imported genuine. Good sound, making good clear sound even when used in wide space
ReplyDeleteGái gọi Mỹ Đình - Đình Thôn, gái gọi Nguyễn Khánh Toàn, gái gọi hà nội, gái gọi Phố Cổ - Quán Sứ