Consider the standard Linux GCC/G++ compiler. Let's know about the command to execute program in GCC/G++ compiler.
./program_name
Here, program_name is the executable file name and we have to print this file name using C program. The parameter which are being passed through the command line can be scanned in the program through command line arguments.
To scan/get the parameters which are supplied through the command line, we use following syntax of the main function
int main(int argc, char *argv[])
Here, argc is an integer type of arguments, which contains the total number of arguments/parameters supplied through the command line and argv[] is an array of character pointer (array of strings), which contains the all parameters.
Here is the list of some of the command line argument based programs in C.
In this program, we are learning how to print program's name? – program's name is ./program_name which is the first (0th index) parameter of the parameter list.
Consider the program
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("program's name is: %s\n",argv[0]);
return 0;
}
Output
program's name is: ./prog
Note:
If you are using a compiler, which has different syntax of executing the program like compiler_name program_name, in that case second (1st index) parameter will be the name of the program.
Consider the standard Linux GCC/G++ compiler. Let's know about the command to execute program in GCC/G++ compiler.
Here, program_name is the executable file name and we have to print this file name using C program. The parameter which are being passed through the command line can be scanned in the program through command line arguments.
To scan/get the parameters which are supplied through the command line, we use following syntax of the main function
Here, argc is an integer type of arguments, which contains the total number of arguments/parameters supplied through the command line and argv[] is an array of character pointer (array of strings), which contains the all parameters.
Here is the list of some of the command line argument based programs in C.
In this program, we are learning how to print program's name? – program's name is ./program_name which is the first (0th index) parameter of the parameter list.
Consider the program
Output
Note:
If you are using a compiler, which has different syntax of executing the program like compiler_name program_name, in that case second (1st index) parameter will be the name of the program.
need an explanation for this answer? contact us directly to get an explanation for this answer