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

标签 c ubuntu

我在 Ubuntu 中使用 Emacs 输入了以下代码并使用命令行进行编译

#include <stdio.h>

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

主函数参数中包含 void 会返回以下警告

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

当我删除括号内的“void”时,程序编译时没有任何错误。该程序中的 main(void) 有什么不正确的地方?

编译命令是 gcc -Wall -ggdb helloworld.c -o 你好

编辑: 这是我想分享的屏幕截图 enter image description here

最佳答案

该错误与 main() 签名中的 void 无关,int main(void) 是正确的当您不需要处理命令行参数时,这是定义 main() 的方法。

该错误意味着您定义了 int main(void) 并且没有从函数返回值。像这样

#include <stdio.h>

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

在较新版本的 gcc 中,已删除 main() 的此警告,因为 main() 隐式返回 0 当程序退出时,除非您使用 exit() 另有指示或从 main() 显式返回。

普通函数仍然会触发此警告,并且它有助于防止由于未从函数返回并 try catch 返回值而导致的未定义行为

关于c - Hello world 程序在 main 中出现 void 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34442705/

相关文章:

c - 将数据从文件写入 C 上的结构

php - PHP 7.0 FastCGI 进程管理器启动失败

python - PyVirtualDisplay属性错误: 'Display' object has no attribute 'old_display_var'

python - 使用python在Linux上更改鼠标灵敏度/dpi

c - 释放 int 指针到指针数组单独工作正常,但在主程序中崩溃

c - 如何在C、C++中输入1字节整数值

c - 使用 printf 时出现段错误

c - pthread_create - 如何创建单线程并在需要时在循环中调用它

ubuntu - 在 Ubuntu 上进行 whois 通配符搜索

c - Ubuntu 中的 FTP 主动模式客户端错误 : I won't open a connection to 127. 0.1.1