Command line arguments in C
Command line arguments in C
The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by the main() functions. In order to support this command line prompt in the C standard, the developer has to change the structure of the main() function of the program or application.
The command line arguments are considered as a parameter that is supplied to the program every time it is invoked. They are an important part of the C programming language and hence are mostly used when one needs to control the programs from the outside rather than from the inside.
C provides programmers to put command-line arguments within the program, which will allow users to add values at the very start of program execution. These command line arguments are often given the name of the application or the program in the operating system like DOS( Disc Operating System) or Linux.
Learning about the declaration of the main() function is more important which has no accepted arguments in the previous times. The main() function can accept two types of arguments from the command line, one being the number of command line arguments and other being the full list of all the arguments of the command line.
Syntax:
int main(int argc, char *argv[]) Where,
- argc- The file name is the first argument, and it counts the number of arguments on the command line. argc is an integer (int) and it stores the name of the program. So if the value of the program is being passed, the value of the argc would be 2 as one will be considered for the argument and the other for the program name. The value of the argc should always be a negative value.
- argv[]- is a pointer that consists of the total number of arguments present in the program and the first argument is the name of the file. In other words Argv[] is an array of characters which has all the arguments.
- If the value of the argc is greater than the value of 0, then the array elements from the argv[0] to argv[argc-1] will contain pointers to strings.
When C programs or applications are executed, it is possible to send values from the command line to them. These values are known as command line arguments, and they are frequently required by programmers and developers. The most important function of the C programming language is the main() function. It is mostly defined with a return type of int and without parameters:
int main() { /* ... */ }
When C programs are being executed, it is possible to send values from the command line to them.
E.g.:
#include <stdio.h> #include <conio.h> int main ( int argc, char *argv[] ) { if ( argc != 2 ) /* argc should be 2 for correct execution */ { /* We print argv[0] assuming it is the program name */ printf( "usage: %s filename", argv[0] ); } else { FILE *file = fopen( argv[1], "r" ); /* fopen returns 0, the NULL pointer, on failure */ if ( file == 0 ) { printf( "Could not open file\n" ); } else { int x; while ( ( x = fgetc( file ) ) != EOF ) { printf( "%c", x ); } fclose( file ); } } }
#include <stdio.h> #include <conio.h> int main(int argc, char *argv[]) { int i; if( argc >= 2 ) { printf("The arguments supplied are:\n"); for(i = 1; i < argc; i++) { printf("%s\t", argv[i]); } } else { printf("The argument list is empty.\n"); } return 0; }
Output:
The argument list is empty.
Remember that argv[0] holds the name of the program and argv[1] points to the first command line argument and argv[n] gives the last argument. If no argument is supplied, argc will be 1.
Properties of Command Line Arguments:
- They are always passed to the main() function.
- Argv[argc] is considered to be a NULL pointer .
- They are parameters or the arguments supplied to the program when it is invoked.
- They are used to control programs from outside instead of hard coding those values inside the code.
- argv[0] holds the name of the program.
- argv[1] points to the first command line argument and argv[n] points to the last argument.
You pass all the command line arguments separated by a space, but if the argument itself has a space then you can pass such arguments by putting them inside double quotes “” or single quotes ”.