Command Line Arguments in C
Command Line Arguments
- The arguments passed from command line are called command line arguments. These arguments are handled by main() function.
- To support command line argument, you need to change the structure of main() function.
Syntax
int main(int argc, char *argv[] )
argc counts the number of arguments. It counts the file name as the first argument.
The argv[] contains the total number of arguments. The first argument is always the file name.
Sample Code:
#include <stdio.h>
#include <conio.h>
void main( int argc, char *argv[] ) {
clrscr();
if( argc == 2 )
{ printf("two argument sent");
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");
}
getch();
}
Output:
![c-fwrite](https://wikitechy.com/cdn/tutorials/c-programming/command-line-argument.gif)
Learn C - C tutorial - Command Line Argument - C examples - C programs