0% found this document useful (0 votes)
2 views3 pages

CommandLine Arguments 1646368805712

The document explains how to use command line arguments in C programs through the main function, detailing the significance of argc and argv. It provides example programs for summing numbers and comparing strings passed as command line arguments, along with expected outputs for various input scenarios. Key points include the requirement of at least one argument and the conversion of character strings to numerical types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

CommandLine Arguments 1646368805712

The document explains how to use command line arguments in C programs through the main function, detailing the significance of argc and argv. It provides example programs for summing numbers and comparing strings passed as command line arguments, along with expected outputs for various input scenarios. Key points include the requirement of at least one argument and the conversion of character strings to numerical types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Command Line Arguments:

When executing a program in C there is a way to pass command line arguments, (i.e. passing arguments to main function
during program invocation) by passing a character arrays each parameter separated by a space.
To use commandline input argument in program, two arguments argc (Number of parameters), argv (Parameter list) are used.

Significance:
• Arguments are always passed to main( ).
• There must be two arguments to main function.
– first is an integer
– second char pointer to an array
• First argument (argv[0]) will always be the name of the calling program.
• argc will always be at least 1
• The first argument is always argv[0]
• The last argument is always argv[argc-1]
• argv[argc] will always be a null pointer
• Arguments are always passed as character strings. Numbers must be converted from characters to integers, floats, doubles, etc.

Example Program 1: To add sum of n numbers given in command line.


#include<stdio.h>
main(int argc, char *argv[])
{
int sum =0, i=0,a,b;
if(argc == 1)
{
printf("No additional input is passed as command line argument\n");
}
else if(argc <3)
{
printf("Too few arguments\n");
}
else if( argc == 3)
{
a = atoi(argv[1]);
b = atoi(argv[2]);
printf("Sum = %d\n",a+b);
}
else
{
for(i=1;i<argc;i++)
{
sum = sum + atoi(argv[i]);
}
printf("Sum of n numbers is %d\n",sum);
}
}
Output:
[priyanka@mepcolinux cmdline]$./a.out
No additional input is passed as command line argument
[priyanka@mepcolinux cmdline]$./a.out 1
Too few arguments
[priyanka@mepcolinux cmdline]$./a.out 1 2
Sum = 3
[priyanka@mepcolinux cmdline]$./a.out 1 2 4 5 6 7
Sum of n numbers is 25

Example Program 2: To compare two strings given in command line.


#include<stdio.h>
main(int argc, char *argv[])
{
if(argc < 3)
{
printf("Too few arguments\n");
}
else if ( argc >3)
{
printf("Too many arguments\n");
}
else
{
if(strcmp(argv[1], argv[2])>0)
printf("First input string %s is greater\n",argv[1]);

else if(strcmp(argv[1], argv[2])<0)


printf("Second input string %s is greater\n",argv[2]);
else
printf("Both strings are equal\n");
}
}
Output:
[priyanka@mepcolinux cmdline]$./a.out
Too few arguments
[priyanka@mepcolinux cmdline]$./a.out abi abu
Second input string abu is greater
[priyanka@mepcolinux cmdline]$./a.out abi abas
First input string abi is greater
[priyanka@mepcolinux cmdline]$./a.out abi abas abu
Too many arguments

You might also like