main function - 主函数

main function - 主函数

Every C program coded to run in a hosted execution environment contains the definition (not the prototype) of a function called main, which is the designated start of the program.
每个要在宿主环境运行的 C 编码程序都含有称作 main 的函数定义 (非原型),它是程序的指定起始点。

int main (void) { body } (1)
int main (int argc, char *argv[]) { body } (2)	

1. Parameters - 参数

argc - Non-negative value representing the number of arguments passed to the program from the environment in which the program is run.
代表程序所运行的环境传递给程序的参数数量。

argv - Pointer to the first element of an array of argc + 1 pointers, of which the last one is NULL and the previous ones, if any, point to strings that represent the arguments passed to the program from the host environment. If argv[0] is not a null pointer (or, equivalently, if argc > 0), it points to a string that represents the program name, which is empty if the program name is not available from the host environment.
指向 argc + 1 个指针的数组的首元素的指针。数组末元素为空指针,而若前面有元素,则它们指向表示从宿主环境传递给程序的参数的字符串。若 argv[0] 不是空指针 (或等价地 argc > 0),则它指向表示程序名的字符串。若程序名从宿主环境不可用则该字符串为空。

The names argc and argv are arbitrary, as well as the representation of the types of the parameters: int main(int ac, char** av) is equally valid.
名称 argc and argv,以及这些参数的类型表示是任意的:int main(int ac, char** av) 同样合法。

A common implementation-defined form of main is int main(int argc, char *argv[], char *envp[]), where a third argument, of type char*[], points at an array of pointers to the host environment variables.
main 的常见实现定义形式是 int main(int argc, char *argv[], char *envp[]),其中第三参数类型为 char*[],指向宿主环境变量的指针的数组。

2. Return value - 返回值

If the return statement is used, the return value is used as the argument to the implicit call to exit() (see below for details). The values zero and EXIT_SUCCESS indicate successful termination, the value EXIT_FAILURE indicates unsuccessful termination.
若使用返回语句,则返回值会用作隐式调用 exit() 的参数。值 zero and EXIT_SUCCESS 指示成功终止,值 EXIT_FAILURE 指示不成功终止。

4. Example

Demonstrates how to inform a program about where to find its input and where to write its results.
演示如何告知程序其寻找输入处及写结果处。

4.1 Example

//============================================================================
// Name        : main
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>

int main(int argc, char *argv[])
{
	printf("argc = %d\n", argc);

	for (int idx = 0; idx < argc; ++idx)
	{
		printf("argv[%d] --> %s\n", idx, argv[idx]);
	}

	printf("argv[argc] = %p\n", (void*)argv[argc]);

	return 0;
}
argc = 1
argv[0] --> D:\visual_studio_workspace\yongqiang\Debug\yongqiang.exe
argv[argc] = 00000000
请按任意键继续. . .

在这里插入图片描述

4.2 Example

Invocation (调用): yongqiang.exe infile outfile

//============================================================================
// Name        : main
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>

int main(int argc, char *argv[])
{
	printf("argc = %d\n", argc);

	for (int idx = 0; idx < argc; ++idx)
	{
		printf("argv[%d] --> %s\n", idx, argv[idx]);
	}

	printf("argv[argc] = %p\n", (void*)argv[argc]);

	return 0;
}

D:\visual_studio_workspace\yongqiang\Debug

Microsoft Windows [版本 10.0.17763.1098]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\Users\cheng>d:

D:\>cd visual_studio_workspace

D:\visual_studio_workspace>cd yongqiang

D:\visual_studio_workspace\yongqiang>cd Debug

D:\visual_studio_workspace\yongqiang\Debug>dir
 驱动器 D 中的卷是 DATA
 卷的序列号是 90CB-45F2

 D:\visual_studio_workspace\yongqiang\Debug 的目录

2020/03/21  21:10    <DIR>          .
2020/03/21  21:10    <DIR>          ..
2020/03/28  21:21            37,376 yongqiang.exe
2020/03/28  21:21           324,664 yongqiang.ilk
2020/03/28  21:21           651,264 yongqiang.pdb
               3 个文件      1,013,304 字节
               2 个目录 189,175,169,024 可用字节

D:\visual_studio_workspace\yongqiang\Debug>
D:\visual_studio_workspace\yongqiang\Debug>yongqiang.exe infile outfile
argc = 3
argv[0] --> yongqiang.exe
argv[1] --> infile
argv[2] --> outfile
argv[argc] = 00000000

D:\visual_studio_workspace\yongqiang\Debug>

在这里插入图片描述

4.3 Example

Invocation (调用): yongqiang.exe cheng yong qiang

//============================================================================
// Name        : main
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>

int main(int argc, char *argv[])
{
	printf("argc = %d\n", argc);

	for (int idx = 0; idx < argc; ++idx)
	{
		printf("argv[%d] --> %s\n", idx, argv[idx]);
	}

	printf("argv[argc] = %p\n", (void*)argv[argc]);

	return 0;
}

D:\visual_studio_workspace\yongqiang\Debug

Microsoft Windows [版本 10.0.17763.1098]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\Users\cheng>d:

D:\>cd visual_studio_workspace

D:\visual_studio_workspace>cd yongqiang

D:\visual_studio_workspace\yongqiang>dir
 驱动器 D 中的卷是 DATA
 卷的序列号是 90CB-45F2

 D:\visual_studio_workspace\yongqiang 的目录

2020/03/28  23:37    <DIR>          .
2020/03/28  23:37    <DIR>          ..
2020/03/21  21:10    <DIR>          Debug
2020/03/28  21:21    <DIR>          yongqiang
2020/03/21  15:55             1,309 yongqiang.sln
2020/03/28  23:37         1,826,816 yongqiang.VC.db
               2 个文件      1,828,125 字节
               4 个目录 189,174,976,512 可用字节

D:\visual_studio_workspace\yongqiang>
D:\visual_studio_workspace\yongqiang>cd Debug

D:\visual_studio_workspace\yongqiang\Debug>dir
 驱动器 D 中的卷是 DATA
 卷的序列号是 90CB-45F2

 D:\visual_studio_workspace\yongqiang\Debug 的目录

2020/03/21  21:10    <DIR>          .
2020/03/21  21:10    <DIR>          ..
2020/03/28  21:21            37,376 yongqiang.exe
2020/03/28  21:21           324,664 yongqiang.ilk
2020/03/28  21:21           651,264 yongqiang.pdb
               3 个文件      1,013,304 字节
               2 个目录 189,174,976,512 可用字节

D:\visual_studio_workspace\yongqiang\Debug>
D:\visual_studio_workspace\yongqiang\Debug>yongqiang.exe cheng yong qiang
argc = 4
argv[0] --> yongqiang.exe
argv[1] --> cheng
argv[2] --> yong
argv[3] --> qiang
argv[argc] = 00000000

D:\visual_studio_workspace\yongqiang\Debug>

在这里插入图片描述

References

https://en.cppreference.com/w/c/language/main_function
http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html

发布了525 篇原创文章 · 获赞 1873 · 访问量 116万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/105168771