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

Command Line Argument

The document discusses command line arguments in C programs. It explains that argc is the argument count and argv is an array of character pointers to each argument. argv[0] contains the program name, and subsequent elements contain any additional arguments passed when the program is run. Examples are provided of C programs that demonstrate how to access and use command line arguments to control program behavior, perform calculations, and return results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Command Line Argument

The document discusses command line arguments in C programs. It explains that argc is the argument count and argv is an array of character pointers to each argument. argv[0] contains the program name, and subsequent elements contain any additional arguments passed when the program is run. Examples are provided of C programs that demonstrate how to access and use command line arguments to control program behavior, perform calculations, and return results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Command line argument

int main(int argc,char *argv[])
argc – Argument count : no of arguments passed
argv– Argument vector: pointer which points to each
argument which is passed to main.
They are passed to main().
They are parameters supplied to the program when it
is invoked.
They are used to control program from outside
instead of hard coding those values inside the code.
argv[0] holds the name of the program.
After that, every element is command line arguments.
argv[argc] is a NULL pointer.
You pass all the command line
arguments seperated by space,but IF
ARGUMENT ITSELF HAS SPACE
then you can pass such arguments
by putting them inside double quotes”
” or single quotes “.
WAP to find the factorial of a
number with the use of
command line argument
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
int i,sum=0;
long int fact=1;
if(argc!=2)
{
printf(“Enter only one parameter\n”);
exit(1);
}
if(atoi(argv[1])==0 ll atoi(argv[1])==1)
fact=1;
else
for(i=2;i<=atoi(argv[1]);i++)
fact=fact*i;
printf(“Factorial of %s is %ld\n”,argv[1],fact);
return 0;
}
 
C program to illustrate the command
line argument
#include<stdio.h>
int main(int argc,char * argv[])
{
int i;
printf(“Program name is : %s”,argv[0]);
if(argc==1)
printf(“No extra parameters passed \n”);
if(argc>=2)
{
printf(“The arguments are:”);
for(i=0;i<argc;i++)
printf(“\nargv[%d]:%s”,i,argv[i]);
}
return 0;
}
Adding two numbers using
command line argument
#include<stdio.h>
void main(int argc,char * argv[])
{
int i, sum=0;
if(argc!=3)
{
printf(“You have two enter two numbers:”);
exit(1);
}
printf(“The sum is: “);
for(i=1;i<argc;i++)
sum=sum+atoi(argv[i]);
printf(“%d”,sum);
}
Program to find area of a circle
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
float area;
if(argc!=2)
{
printf(“Enter only radius of circle\n”);
exit(1);
}
area=3.14*atoi(argv[1])*atoi(argv[1]);
printf(“Factorial of  circle with radius %s is %f\n”,argv[1],area);
return 0;
}
 
Program to find whether a
number is prime or not
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
int n,i,f=0;
if(argc!=2)
{
printf(“Enter number\n”);
exit(1);
}
 n= atoi(argv[1]);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
printf(“The number is not prime\n”);
f=1;
break;
}
}
if(f==0)
{
printf(“Number is prime\n”);
}
return 0;
}
 
 
Program to find whether a given
year is leap or not
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
int year;
if(argc!=2)
{
printf(“Enter  year\n”);
exit(1);
}
 year= atoi(argv[1]);
if(year%4==0)
{
if(year%100==)
{
if(year%400==0)
printf(“%d is a leap year”,year);
else
printf(“%d is not a leap year”,year);
}
}
else
printf(“%d is a leap year”,year);
}
else
printf(“%d is not a leap year”,year);
return 0;
}
 
 
 
Program to find GCD of two
numbers
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
int n1,n2;
if(argc!=3)
{
printf(“Enter  two numbers\n”);
exit(1);
}
 n1= atoi(argv[1]);
n2= atoi(argv[2]);
for(i=1;i<=n1 &&i<=n2;++i)
{
if(n1%i==0 && n2%i==0)
gcd=i;
}
printf(“Gcd of %d and %d is %d”, n1,n2,gcd);
return 0;
}
 
 

You might also like