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

Command Line

Command line arguments allow programs to accept input values when run. The main() function contains argc, which is the number of arguments, and argv, which is an array of argument strings. A sample C program demonstrates printing the number of arguments and looping through argv to print each argument value passed on the command line.

Uploaded by

ash
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Command Line

Command line arguments allow programs to accept input values when run. The main() function contains argc, which is the number of arguments, and argv, which is an array of argument strings. A sample C program demonstrates printing the number of arguments and looping through argv to print each argument value passed on the command line.

Uploaded by

ash
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Command line

arguments
Command line arguments
• Here is the syntax of main() function with command line
arguments
• int main( int argc, char *argv[] ) Here,
• int argc - is the local variable name (you can use any name
here), basically argc is the argument count, it will contain
the total number of arguments given through the
command line (including program name).
• char *argv[ ] - An array of strings, that will contain the
strings (character arrays) which is written on the command
line.
• Consider the following statement
• ./main Mike 27 "New Delhi" India
• Here, total number of arguments will be 5.
Department of CSE 2
Example
#include<stdio.h>
int main(int argc, char* argv[ ])
{
int iLoop;
printf("\nTotal number of arguments : %d",argc);
printf("\nArguments are :\n");
for(iLoop=0;iLoop < argc; iLoop++)
{
printf("%s\t",argv[iLoop]);
}
printf("\n");
return 0;
}

Department of CSE 3
Sum of two Integers
#include <stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int a,b,sum;
if(argc!=3)
{
printf("please use \"prg_name value1 value2 \"\n");
return -1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
sum = a+b;
printf("Sum of %d, %d is: %d\n",a,b,sum);
return 0;
}

Department of CSE 4

You might also like