C语言 int main() 和 void main() 如何工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18928279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:30:32  来源:igfitidea点击:

how does int main() and void main() work

cvoid

提问by user2106271

I am a beginner in C language. Can anyone explain in detail using example how main(),int main(), void main(), main(void), void main(void), int main(void) work in C language? As in what is happeneing when we use void main() and what is happening when i use int main() in simple language and so on.
I know but cant understand what is it doing:

我是C语言的初学者。任何人都可以使用示例详细解释 main(),int main(), void main(), main(void), void main(void), int main(void) 在 C 语言中是如何工作的吗?就像我们使用 void main() 时发生的情况以及我在简单语言中使用 int main() 时发生的情况一样。
我知道但无法理解它在做什么:

  1. main() - function has no arguments
  2. int main() - function returns int value
  3. void main() - function returns nothing etc.
  1. main() - 函数没有参数
  2. int main() - 函数返回 int 值
  3. void main() - 函数不返回任何内容等。

when i write simple hello world using the int main() return 0 it still gives me the same output as when using void main()) so how does it work? What is its application?

当我使用 int main() return 0 编写简单的 hello world 时,它仍然给我与使用 void main()) 时相同的输出,那么它是如何工作的?它的应用是什么?

回答by Carl Norum

Neither main()or void main()are standard C. The former is allowed as it has an implicit intreturn value, making it the same as int main(). The purpose of main's return value is to return an exit statusto the operating system.

无论main()或者void main()是标准C.前者被允许,因为它有一个隐含的int返回值,使它一样int main()main的返回值的目的是向操作系统返回退出状态

In standard C, the only valid signatures for mainare:

在标准 C 中,唯一有效的签名main是:

int main(void)

and

int main(int argc, char **argv)

The form you're using: int main()is an old style declaration that indicates maintakes an unspecified number of arguments. Don't use it - choose one of those above.

您正在使用的形式:int main()是一种旧式声明,表示main采用未指定数量的参数。不要使用它 - 选择上述之一。

回答by Ron Nuni

If you really want to understand ANSI C 89, I need to correct you in one thing; In ANSI C 89 the difference between the following functions:

如果你真的想了解 ANSI C 89,我需要纠正你一件事;在 ANSI C 89 中,以下函数之间的区别:

int main()
int main(void)
int main(int argc, char* argv[])

is:

是:

int main()

int main()

  • a function that expects unknown number of arguments of unknown types. Returns an integer representing the application software status.
  • 一个需要未知数量的未知类型参数的函数。返回一个表示应用软件状态的整数。

int main(void)

int main(void)

  • a function that expects no arguments. Returns an integer representing the application software status.
  • 一个不需要参数的函数。返回一个表示应用软件状态的整数。

int main(int argc, char * argv[])

int main(int argc, char * argv[])

  • a function that expects argc number of arguments and argv[] arguments. Returns an integer representing the application software status.
  • 一个需要 argc 参数数量和 argv[] 参数的函数。返回一个表示应用软件状态的整数。

About when using each of the functions

关于使用各功能时

int main(void)

int main(void)

  • you need to use this function when your program needs no initial parameters to run/ load (parameters received from the OS - out of the program it self).
  • 当您的程序不需要初始参数来运行/加载(从操作系统接收的参数 - 程序本身之外)时,您需要使用此功能。

int main(int argc, char * argv[])

int main(int argc, char * argv[])

  • you need to use this function when your program needs initial parameters to load (parameters received from the OS - out of the program it self).
  • 当您的程序需要加载初始参数(从操作系统接收的参数 - 程序本身之外)时,您需要使用此函数。

About void main()

关于 void main()

In ANSI C 89, when using void mainand compiling the project AS -ansi -pedantic(in Ubuntu, e.g) you will receive a warning indicating that your main function is of type void and not of type int, but you will be able to run the project. Most C developers tend to use int main()on all of its variants, though void main()will also compile.

在 ANSI C 89 中,当使用void main和编译项目 AS -ansi -pedantic(例如在 Ubuntu 中)时,您将收到一条警告,指出您的主要函数是 void 类型而不是 int 类型,但您将能够运行该项目。大多数 C 开发人员倾向于使用int main()它的所有变体,但void main()也会编译。