我运行以下C代码并得到一个警告:控制到达非void函数的结束
int main(void) {}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
Pas*_*uoq 11
作为添加return
语句的明显解决方案的替代方法main()
,您可以使用C99编译器(如果您使用GCC,则使用"gcc -std = c99").
在C99中,main()
没有return
声明是合法的,然后最终}
隐式返回0.
$ gcc -c -Wall t.c
t.c: In function ‘main’:
t.c:20: warning: control reaches end of non-void function
$ gcc -c -Wall -std=c99 t.c
$
Run Code Online (Sandbox Code Playgroud)
一张纸条,纯粹主义者会认为重要的是:你应该不会通过声明固定预警main()
为返回类型void
.
dre*_*ash 10
只需return 0
加入你的main()
.你的函数main返回一个int(int main(void)
)因此你应该在它的末尾添加一个return.
控制到达非空函数的末尾
Problem: I received the following warning:
Run Code Online (Sandbox Code Playgroud)
警告:控制到达非void函数的结束
解决方案:此警告类似于Return中没有值的警告.如果控制到达函数的末尾并且没有遇到返回,则GCC假定返回没有返回值.但是,为此,该函数需要返回值.在函数结束时,添加一个返回合适返回值的return语句,即使控件永远不会到达那里.
方案:
int main(void)
{
my_strcpy(strB, strA);
puts(strB);
return 0;
}
Run Code Online (Sandbox Code Playgroud)