catalogue
- 1, main function writing
- 2, Introduction to main function parameters
- 3, Use main function parameters
- 3, Guess you like it
Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language
1, main function writing
Defined in C99 standard main function Two correct ways to write
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C language main function parameter main(int argc, char *argv []) //@Time:2021/07/16 07:30 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ int main(void) { /* ... */ } int main(int argc, char *argv[]) { /* ... */ }
2, Introduction to main function parameters
/* Parameter introduction argc : main The number of function parameters. When the parameter is void, argc=1. The default parameter is the executable file name argv : Pointer array, respectively pointing to the first address of a parameter string, where argv[0] points to the default parameter */ int main(int argc, char *argv[]) { /* ... */ }
By default, the argc value is 1, which means argv[0] the file name of the current project executable; The following parameters are stored in the character array in order starting from arg[1], and the number of argc is at least 1;
3, Use main function parameters
1. Print main function parameters
To avoid a flash of the console program, we can use** system("pause") **After waiting for user input, end the program;
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C language main function parameter main(int argc, char *argv []) //@Time:2021/07/16 07:30 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include <stdio.h> #include <windows.h> int main(int argc,char *argv[]) { int ii=0; // Displays the number of parameters printf("argc is %d\n",argc); // List all parameters for (ii=0;ii<argc;ii++) { printf("argv[%d] is %s\n",ii,argv[ii]); } system("pause"); } /* Output: argc is 1 argv[0] is C:\Users\Administrator\Desktop\mainFunc\Debug\mainFunc.exe Please press any key to continue */
By default, the argc value is 1, which means argv[0] the file name of the current project executable;
a. Run the exe file directly
argc is 1 argv[0] is C:\Users\Administrator\Desktop\mainFunc\Debug\mainFunc.exe Please press any key to continue. . .
b. Open the cmd command line window and execute the exe file
c. Open the cmd command line window, execute the exe file and set the main function parameter
Note: there is a space between exe and each subsequent parameter;
2. Use the main function parameter
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C language main function parameter main(int argc, char *argv []) //@Time:2021/07/16 07:30 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include<stdio.h> #include<string.h> #include<stdlib.h> void Add(int a, int b) { printf("%d\n", a + b); } void Mul(int a, int b) { printf("%d\n", a*b); } void Sub(int a, int b) { printf("%d\n", a - b); } void Div(int a, int b) { printf("%d\n", a / b); } int main(int argc, char *argv[]) { if (argc < 4) { printf("The input format is incorrect. Please input according to the specified format, for example: xxx.exe -a 4 5 \n"); system("pause"); return 0; } int a = atoi(argv[2]); int b = atoi(argv[3]); if (strcmp("-a", argv[1]) == 0) { Add(a, b); } else if (strcmp("-s", argv[1]) == 0) { Sub(a, b); } else if (strcmp("-m", argv[1]) == 0) { Mul(a, b); } else if (strcmp("-d", argv[1]) == 0) { Div(a, b); } system("pause"); return 0; } /* Directly executing the exe file will prompt: The input format is incorrect. Please input according to the specified format, for example: xxx.exe -a 4 5 Please press any key to continue The error is reported because we have not set the input parameters for the main function. Open the cmd window to set the parameters!! */
Note: there is a space between exe and each subsequent parameter;
3, Guess you like it
- Difference between C language array subscript out of bounds and memory overflow
- C language uses pointers to traverse arrays
- Difference between C language pointer and array
- Difference between C language pointer array and array pointer
- C language field pointer
- C language function value transfer and address transfer
- C language function indefinite length parameter
- C language function pointer
- C language pointer function
- C language callback function callback
- C language #pragma once
- Difference between C language #include < > and #include ""
- C language const modifier function parameters
- Difference between const and define in C language
- C language # operators
- C language ## operators
- C language__ VA_ARGS__
- C language##__ VA_ARGS__
- C language function indefinite length parameter##__ VA_ARGS__ Classic case
- C language va_start macro
- C language va_end macro
- C language va_arg macro
- C language vprintf function
- C language va_start / va_end / va_arg custom printf function
- C language main function
- C language main function parameter main(int argc, char *argv [])
No reprint without permission: Ape programming ยป C language main function parameter main(int argc, char *argv [])