我研究并理解在 C 中创建或编写函数的标准方法是:
function declaration - declare the function name, arguments, and return value
function definition - define the function with the actual code implementation
function call - call or invoke the function with the name and arguments
但是我对函数
main()
的使用感到困惑。 .是否允许在 C 中使用 main()
在所有代码块中,即使用 main()
在声明中,使用 main()
在定义和使用 main()
在通话中。请帮忙,我的考试是下个月,我很困惑。
最佳答案
函数main()
在 C 中是一个特殊的函数名称,它指示您的程序从哪里开始,您编写的 C 源代码在您的程序中的入口点。 C 标准运行时的一部分是需要知道您正在编写的 C 程序从何处开始的入口点。这用函数名 main()
表示。 .
首先,您必须了解 C 编译器将 C 源代码文本转换为二进制目标文件。然后将这些二进制对象文件与其他二进制对象文件链接在一起以生成实际的可执行程序。这些类型的二进制对象文件之一是库文件,其中包含一个或多个函数的二进制代码,这些函数先前已编译并组合到单个文件中。
C 编译器附带几个库文件,其中包含完成可执行程序所需的功能。其中之一是编译器的 C 运行时。
当您的可执行程序启动时,它不会从 main()
开始。 .相反,它从 C 运行时的一个特殊位置开始,为您的可执行文件设置运行时环境,然后调用 main()
您提供的功能。 main()
函数是您的源代码开始的地方。
作为 C 编译器的一部分,还有一个包含文件,例如 #include <stdio.h>
。和别的。这些包含文件包含 C 标准库函数的声明,例如 printf()
功能。
所以main()
在 C 编译器中为您声明了函数,但是您必须提供函数的实际定义。
由于main()
是一个外部标签,只能有一个名为 main()
的函数你定义的。如果您对 main()
有多个定义那么当链接器试图将你的二进制目标文件链接成一个可执行文件时,它会看到多个 main()
函数并发出错误,因为它不知道要使用哪一个。
一种思考方式是:(1) C 编译器为 main()
提供声明。 , (2) 您提供 main()
的定义(3) C 运行时调用你的函数 main()
作为启动程序的一部分。
另见 What is the difference between - 1) Preprocessor,linker, 2)Header file,library? Is my understanding correct?
main()
的函数定义
C 的标准为 main()
指明了两种不同的声明。 : int main(int argc, char *argv[])
和 int main(void)
(见 section 5.1.2.2.1 Program startup in draft ISO/IEC 9899:TC3)。另见 What does int argc, char *argv[] mean? .另见 What should main() return in C and C++? 中的各种讨论。 .
一些 C 编译器还可能提供其他替代方案,包括宽字符界面,例如 int wmain(int argc, wchar_t *argv[])
。 (见 WINMAIN and main() in C++ (Extended))。 C++ 编译器可能允许 int main(int argc, wchar_t *argv[])
因为 C++ 允许函数重载,而 C 不允许。 Microsoft C++ 编译器有多种 main()
替代方案取决于目标程序是控制台应用程序还是 GUI。main()
的返回值的正常使用方式如果成功则返回值 0,如果出现错误则返回非零正数。这是原始 Unix 环境中的正常和可接受的行为,程序从命令行在终端窗口中运行(参见 Wikipedia topic Bourne Shell 和 Wikipedia topic Bash 以及包含一些脚本示例的 Returning value from called function in a shell script),并且大多数程序都是与命令 shell 脚本一起使用。
一些程序使用返回值来指示某种测试的结果。在这些情况下,main()
返回的值不是成功或错误代码,而是返回值指示脚本可以测试的某些条件,作为执行其目的的一部分。例如,从传感器查询空气质量指示的程序可能会返回三个不同的值,指示好、一般或差,然后脚本使用这些值来执行某些操作,例如以特定速度打开通风系统风扇。
以下是 main()
的几个示例定义:
// usage: mypgm pathname
// Open the specified file and do things with the file's contents.
//
// program expects a single command line argument specifying a file.
// if pathname is not specified then it's an error.
int main(int argc, char *argv[])
{
// there is always one argument as the first argument is always
// the program name. Any additional arguments added to the
// command line begin with the second argument.
// argv[0] -> program or command name
// argv[1] -> expecting a pathname to be specified
if (argc < 2) {
printf ("Filename not specified\n");
return 1; // return non-zero indicating an error
}
// source code to do stuff with the specified file
return 0; // return zero indicating no error
}
// usage: mypgm
//
// program does not expect any command line arguments and if any are
// specified the arguments are ignored.
//
int main (void)
{
// source code to do stuff
return 0;
}
// usage: mypgm
//
// program does not expect any command line arguments and if any are
// specified the arguments are ignored.
//
// the following definition also compiles.
int main ()
{
// source code to do stuff
return 0;
}
关于c - C 中 main() 的函数声明和定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39169717/