Command line arguments in C
It is possible to pass some arguments from the command line
to your C programs when they are executed. These arguments are called command line arguments. They are,
- argc
- argv[]
where,
argc
--> Number of
arguments in the command line including program name
argv[] –> This is carrying all the arguments
argv[] –> This is carrying all the arguments
- In real time application, it will happen to pass
arguments to the main program itself. These arguments are passed to
the main () function while executing binary file from command line.
Example1:
#include<stdio.h>
#include<conio.h>
main(int
argc, char *argv[])
{
int i;
clrscr();
if(argc==1)
{
printf("\n
Pass the arguments more than one");
exit(1);
}
for(i=1;
i<argc; i++)
printf("%s",argv[i]);
}
Example2:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main(int
argc, char *argv[])
{
int i, n, sum=0;
clrscr();
if(argc==1)
{
printf("\n
Pass the arguments more than one");
exit(1);
}
for(i=1;
i<argc; i++)
{
n=atoi(argv[i]);
sum=sum+n;
}
printf("\n
Total Sum is = %d",sum);
}
No comments:
Post a Comment