C语言 未定义返回类型的 main() 函数给出警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18158541/
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
main() function defined without return type gives warning
提问by user2669749
This is my program:
这是我的程序:
main()
{
printf("hello world\n");
}
I get this warning when compiling it:
编译时我收到此警告:
function should return a value
When changing main()to void main(), the warning disappears.
更改main()为 时void main(),警告消失。
Why is that so?
为什么呢?
回答by 0decimal0
There are few things which you should take note of :
您应该注意以下几点:
- The
intis themain()function's return type. That means that the kind of valuemain()can return is an integer. main( )was tolerated by the C90 compilers but not by C99 compilers which means its not a part of C99 standard anymore , so don't do this.void main()is not a standard form ,some compilers allow this, but none of the standards have ever listed it as an option. Therefore, compilers don't have to accept this form, and several don't. Again, stick to the standard form, and you won't run into problems if you move a program from one compiler to another.And one last thing , instead of writing main like this :
int main()// here you are being silent about passing arguments to main , meaning it may or may not take arguments
- 该
int是main()函数的返回类型。这意味着main()可以返回的值类型是整数。 main( )被 C90 编译器容忍,但不被 C99 编译器容忍,这意味着它不再是 C99 标准的一部分,所以不要这样做。void main()不是标准形式,一些编译器允许这样做,但没有一个标准将其列为选项。因此,编译器不必接受这种形式,有些则不必。同样,坚持标准形式,如果将程序从一个编译器移动到另一个编译器,就不会遇到问题。最后一件事,而不是像这样写 main :
int main()//在这里,您对将参数传递给 main 保持沉默,这意味着它可能会也可能不会接受参数
write like this :
像这样写:
int main(void)// this specifies there are no arguments taken by main
You might wanna look at theC99 standardfor further details.
您可能想查看C99 标准以获取更多详细信息。
回答by Keith Thompson
Quick summary: If you don't want to use command-line arguments, you should write:
快速总结:如果你不想使用命令行参数,你应该写:
int main(void) {
/* body of main function */
}
If you do:
如果你这样做:
int main(int argc, char *argv[]) {
/* ... */
}
These are the onlyportable ways to define the mainfunction.
这些是定义函数的唯一可移植方式main。
You should probablyhave a return 0;at the end, though it's not strictly necessary. Returning 0indicates successful execution. There are ways to indicate that execution failed; I won't get into that here.
您可能应该return 0;在最后有一个,尽管这不是绝对必要的。返回0表示执行成功。有多种方法可以表明执行失败;我不会在这里讨论。
There's some history behind this. The rules for a valid definition of the mainfunction have changed a bit across different releases of the C standard.
这背后有一些历史。main函数的有效定义规则在 C 标准的不同版本中略有变化。
Before the introduction of the first official standard for C in 1989, the most common form was:
在 1989 年引入第一个 C 官方标准之前,最常见的形式是:
main()
{
/* ... */
}
Or, if you wanted to use command-line arguments:
或者,如果您想使用命令行参数:
main(argc, argv)
/* argc is implicitly of type int */
char *argv[];
{
/* ... */
}
There was no way to define a function that didn't return a value. If you didn't specify a return type, it defaulted to int.
没有办法定义一个不返回值的函数。如果您没有指定返回类型,则默认为int.
The 1989 ANSI C standard (which was republished with editorial changes as the 1990 ISO C standard) introduced prototypes, function declarations and definitions that specify the parameter types. There are two equally valid definitions for main. You can use one or the other depending on whether you need to use command line arguments:
1989 年的 ANSI C 标准(作为 1990 年的 ISO C 标准在编辑更改后重新发布)引入了指定参数类型的原型、函数声明和定义。有两个同样有效的定义main。根据是否需要使用命令行参数,您可以使用一种或另一种:
int main(void) {
/* ... */
}
or
或者
int main(int argc, char *argv[]) {
/* ... */
}
(char *argv[]can also be written as char **argv. This rule applies only to parameter definitions.)
(char *argv[]也可以写成char **argv。此规则仅适用于参数定义。)
A given compiler may or may not choose to permit other forms. For example, some compilers support a third parameter envp.
给定的编译器可能会也可能不会选择允许其他形式。例如,一些编译器支持第三个参数envp。
Somehow, some authors have gotten the idea that void main()or void main(void)is valid. It can be valid for some particular compiler, but only if that compiler explicitly supports it. It's not portable. The odd thing about this is that the same standard that first introduced the voidkeyword simultaneously established the rule that main's return type is int.
不知何故,一些作者已经得到void main()或void main(void)有效的想法。它可以对某些特定的编译器有效,但前提是该编译器明确支持它。它不便携。奇怪的是,首先引入void关键字的同一标准同时建立了main返回类型为的规则int。
void main()is useful as an indicator that the author of the book you're reading doesn't know the C language very well, and that you should find another book.
void main()可用作指示您正在阅读的书的作者不太了解 C 语言的指标,您应该找另一本书。
The story is different for "freestanding" (embedded) systems. For such systems, the program's entry point is entirely implementation-defined, and might not even be called main. Defining it as void main(void)may well be valid for such systems.
“独立式”(嵌入式)系统的情况有所不同。对于此类系统,程序的入口点完全由实现定义,甚至可能不会被调用main。将其定义为void main(void)可能对此类系统有效。
The 1999 ISO C standard dropped the "implicit int" rule. Taking advantage of that rule was probably never a good idea in the first place. As of ISO C 1990, you could legally use:
1999 年的 ISO C 标准放弃了“隐式 int”规则。一开始,利用这条规则可能从来都不是一个好主意。从 ISO C 1990 开始,您可以合法使用:
main(void) { /* ... */ }
because it was equivalent to:
因为它相当于:
int main(void) { /* ... */ }
As of the 1999 standard, the intis mandatory.
从 1999 年的标准开始,int是强制性的。
The 1999 standard also added a special-case rule: reaching the closing }of the mainfunction is equivalent to executing return 0;. It's still not a bad idea to add the explicit return 0;, especially if your code might be compiled with a pre-C99 compiler.
1999年的标准还增加了一个特殊情况规则:达到闭幕}的的main作用是相当于执行return 0;。添加显式仍然不是一个坏主意return 0;,特别是如果您的代码可能是使用 C99 之前的编译器编译的。
The 2011 ISO C standard didn't make any changes in this area.
2011 ISO C 标准在这方面没有做任何改变。
The difference between int main()and int main(void)is that the latter explicitly says that maintakes no arguments; the former doesn't specify how many arguments it takes. Use the int main(void)form. There have been debates about whether int main()is even legal.
int main()和之间的区别在于int main(void)后者明确表示main不需要参数;前者没有指定需要多少个参数。使用int main(void)表格。是否int main()合法一直存在争议。
You can likely get away with writing void main(), since it's an error that compilers are not actually required to diagnose (it's undefined behaviorunless the implementation documents it).
您可能可以避免编写void main(),因为编译器实际上不需要诊断错误(这是未定义的行为,除非实现对其进行记录)。
The bottom line: The proper definition of mainhas a long and varied history, and there are a lot of variant forms you can probably get away with using. But unless you're programming for an embedded system, there is no point in using anything other than one of the two officially valid forms:
底线: 的正确定义main具有悠久而多样的历史,并且您可能可以使用许多变体形式。但是除非您为嵌入式系统编程,否则除了使用两种正式有效形式之一之外的任何其他形式都没有意义:
int main(void) { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }
回答by Ahmad Saleh
write
写
return 0 ;
at the last line.
在最后一行。
回答by Bad Wolf
c automatically implies the datatype intto functions with no declared datatype. So as far as the compiler is concerned the above is:
c 自动将数据类型隐含int给没有声明数据类型的函数。因此,就编译器而言,上述内容是:
int main()
{
printf("hello world\n");
}
This expects that you would return an integer at the end of it with a returnstatement. If you explicitly specify it as void main()you are telling the compiler that the function does not have a return value, hence no warning.
这期望您在return语句的末尾返回一个整数。如果您void main()在告诉编译器该函数没有返回值时明确指定它,则不会发出警告。
The reason that this is not an error is that if not specified, main()will return 0;at the end of execution. However the compiler is still giving you a warning that this is happening.
这不是错误的原因是如果未指定,main()将return 0;在执行结束时。但是,编译器仍然会警告您正在发生这种情况。
Best practice is to use int main()and then return 0at the end of your program execution like this.
最佳做法是使用int main()然后return 0在程序执行结束时像这样。
int main()
{
printf("hello world\n");
return 0;
}
See: this questionfor more information.
请参阅:此问题以获取更多信息。
回答by Yu Hao
You got the warning because you didn't specify the return type of main.
您收到警告是因为您没有指定main.
You should always use int main, and return an intnumber, usually 0for success.
您应该始终使用int main, 并返回一个int数字,通常是0为了成功。
int main()
{
printf("hello world\n");
return 0; //you can omit this since C99
}
Using void mainon a hosted environment(normally we are, if not, the following doesn't have to be true) leads to undefined behavior, even though it works in some compilers, neveruse it.
使用void main在托管环境(通常我们,如果没有,下面不一定是真实的)导致未定义的行为,即使它工作在一些编译器,从来没有使用它。
The standard says mainhas two kinds of prototype, both returns int:
标准说main有两种原型,都返回int:
C11 5.1.2.2.1 Program startup
C11 5.1.2.2.1程序启动
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
程序启动时调用的函数名为 main。实现声明没有此函数的原型。它应定义为返回类型为 int 且不带参数:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
或带有两个参数(此处称为 argc 和 argv,尽管可以使用任何名称,因为它们对于声明它们的函数而言是本地的):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner.
或等价物;10) 或以其他一些实现定义的方式。

