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:
- In Python sys.argv Usage of
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)
- C language write() function analysis: write failed bad address
- The C language qsort() function reports an error for overflow of – 2147483648 and 2147483648
- Usage and examples of three important functions of tidyr package in R language: gather, spread and separate
- Error c2137 of C language: empty character constant (Fixed)
- The usage and difference of atoi() and stoi() functions in C + +
- Usage of pause function in MATLAB
- Explain stdin, stdout, stderr in C language
- C language string processing error warning, c4996, sprintf, predicted, c4996, strcpy, c4996, strcat
- Solution to error [error] LD returned 1 exit status in C language
- C language error: stray ‘\ 240’ in program|
- Solving the problem of saving object set by save() function in R language
- C language — to solve the problem of program flashback when programming (in VS)
- Solution to the segmentation fault of single chain table in C language
- R language notes – sample() function
- 【.Net Core】using declarations‘ is not available in C# 7.3. Please use language version 8.0 or greate
- The usage of Matlab function downsample
- Type definition error – one of the causes of type definition errors is WM in Oracle_ Concat function usage
- On the coercion of C language