简体   繁体   English

Hello world 程序在 main 中出现 void 错误

[英]Hello world program gives an error on void in main

I have typed in the following code in Ubuntu using Emacs and compiled using the command line我在 Ubuntu 中使用 Emacs 输入了以下代码并使用命令行编译

#include <stdio.h>

int main(void)
{
  printf("Hello World!\n\n");
  return 0;
}

Having the void in the main function argument returns the following warning在 main 函数参数中有 void 返回以下警告

helloworld.c: In function ‘main’:
helloworld.c:6:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

When I removed the "void" within the parenthesis the program compiled without any errors.当我删除括号内的“void”时,程序编译没有任何错误。 What is incorrect in have main(void) in this program?在这个程序中有main(void)有什么不正确?

Compilation command is gcc -Wall -ggdb helloworld.c -o hello编译命令是gcc -Wall -ggdb helloworld.c -o hello

Edit: This is the screenshot which I would like to share编辑:这是我想分享的截图在此处输入图片说明

The error doesn't have anything to do with the void in the signature of main() int main(void) is correct and is the way to define main() when you don't need to handle command line arguments.该错误与main()的签名中的void没有任何关系int main(void)是正确的,并且是在不需要处理命令行参数时定义main()的方法。

The error means that you defined int main(void) and you did not return a value from the function.该错误意味着您定义了int main(void)并且您没有从函数返回值。 Like this像这样

#include <stdio.h>

int main(void) 
{
    printf("Hello World!\n");    
}

This warning was removed for main() in newer versions of gcc because main() implicitly returns 0 when the program exits unless you indicate otherwise with exit() or an explict return from main() .在较新版本的gcc 中main()删除了此警告,因为main()在程序退出时隐式返回0 ,除非您用exit()main()的显式返回另有指示。

Normal functions still trigger this warning and it helps in prevention of undefined behavior due to not returning from a function and trying to capture the return value.普通函数仍会触发此警告,它有助于防止由于未从函数返回并尝试捕获返回值而导致的未定义行为

The only way that warning could be for main() function is if you don't have a return statement and you are compiling in C89/C90 mode.警告可能是针对main()函数的唯一方法是如果您没有 return 语句并且您正在 C89/C90 模式下编译。

Since C99, a return statement at the end of main() is not required and it'll be assumed to return 0;从 C99 开始,不需要在main()末尾的 return 语句,它会被假定为return 0; if control returns from main() 's end.如果控制从main()的末尾返回。 So compile in c99 or C11 mode:所以在 c99 或 C11 模式下编译:

gcc -Wall -ggdb -std=c11 helloworld.c -o hello

which won't trigger that warning.这不会触发该警告。 Or make sure you have return statement if you are compiling in c89/C90.或者,如果您在 c89/C90 中编译,请确保您有return语句。 Until recently (at least upto gcc 4.9), the default mode in gcc is gnu90 .直到最近(至少到 gcc 4.9),gcc 中的默认模式是gnu90 So you would get that warning withot a return statement if you don't pass std= .因此,如果您不通过std=您将在没有 return 语句的情况下收到该警告。

I presume you don't actually have return 0;我想你实际上没有return 0; in your real code that produces this warning as it wouldn't matter in what mode of C you are compiling it if you had an explicit return statement.在产生此警告的实际代码中,因为如果您有明确的return语句,那么在什么 C 模式下编译它都无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM