C language main function parameter main(int argc, char *argv[]) - C language zero basic introductory tutorial

catalogue

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

  1. Difference between C language array subscript out of bounds and memory overflow
  2. C language uses pointers to traverse arrays
  3. Difference between C language pointer and array
  4. Difference between C language pointer array and array pointer
  5. C language field pointer
  6. C language function value transfer and address transfer
  7. C language function indefinite length parameter
  8. C language function pointer
  9. C language pointer function
  10. C language callback function callback
  11. C language #pragma once
  12. Difference between C language #include < > and #include ""
  13. C language const modifier function parameters
  14. Difference between const and define in C language
  15. C language # operators
  16. C language ## operators
  17. C language__ VA_ARGS__
  18. C language##__ VA_ARGS__
  19. C language function indefinite length parameter##__ VA_ARGS__ Classic case
  20. C language va_start macro
  21. C language va_end macro
  22. C language va_arg macro
  23. C language vprintf function
  24. C language va_start / va_end / va_arg custom printf function
  25. C language main function
  26. 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 [])

Keywords: C

Added by leena on Sun, 31 Oct 2021 17:05:43 +0200