You can pass command line argument in C programming.
The command line argument is handled by the program main function where argc[] is total number of arguments passed, and argv[] is a array which points number of parameters passed in the program.
#include <stdio.h>
int main( int argc, char *argv[] ) {
if( argc == 2 ) {
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");
}
}
argv[] is the program name and argv[1] is like pointer to the command line, and *argv[n] is the last argument.
#include <stdio.h>
int main( int argc, char *argv[] ) {
printf("Program name %s\n", argv[0]);
if( argc == 2 ) {
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");
}
}
0 Comment(s)