int main(int argc, char *argv[])
So what are argc and Argv [] for?
Where argc is the number of arguments entered externally and argv[] is the string array of arguments. This may not be obvious to you, but let’s take a look at an example of the C file argtest.c shown below
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("argc is %d\n",argc);
for(int i=0;i<argc;i++)
{
printf("argv[%d] is: %s\n",i,argv[i]);
}
return 0;
}
These lines of code are simple, first printing the value of argc, then printing out the string of all argv[] arrays.
Use the following command to compile the C file
gcc argtest.c -o argtest
After compiling to produce the executable file argtest, execute the following command
./argtest
The output result of the program is
argc is 1
argv[0] is: ./argtest
This indicates that when the program is executed, only one parameter is entered, and this parameter is the command executing the program.
Execute the following command
./argtest 1234 abcd
The output result of the program is
argc is 3
argv[0] is: ./argtest
argv[1] is: 1234
argv[2] is: abcd
This indicates that the program entered three arguments, and that the last two space-separated strings of the command were passed to the main function.
Through argc and Argv [] we can pass arguments to the program by command.
Read More:
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)
- error: a label can only be part of a statement and a declaration is not a statement (How to Fix)
- [Warning] incompatible implicit declaration of built-in function ‘strcat’
- C + + pauses the black window system (“pause”); (get ch(), getchar(), system (pause)’s connection and difference
- C / C + + library function (tower / tower) realizes the conversion of letter case
- “Error: ` cout ‘was not declared in this scope”
- The language of C__ FILE__ 、__ LINE__ And line
- Errno in Linux Programming
- Solving the problem of Chinese garbled code in qtring
- Import sys module
- Error c2137 of C language: empty character constant (Fixed)
- C language write() function analysis: write failed bad address
- Configuring OpenGL in Visual Studio 2015
- To solve the problem of flashback of calling class function of glutsolidcube() in Win32 program
- Solve the problem of error: cannot pass objects of non trivially copyable type ‘STD:: String’ in C / C + +
- Conversion from hexadecimal to decimal
- Vs2017 installing OpenGL
- g++ error: ‘printf’ was not declared in this scope
- In Python sys.argv Usage of
- Use of C + + ifstream and error handling