C语言中void函数中的return语句

Jay*_*esh -1 c return function void

在以下程序中,为什么编译器不会给出任何错误或警告?

cc -Wall -pedantic my_program.c
Run Code Online (Sandbox Code Playgroud)

代码在这里:

#include <stdio.h>

void f()
{
        return; // return statement in void function
}

int main()
{
        f();
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在Linux平台上用GCC编译器编译了程序.

Bat*_*eba 7

当然你可以return;在一个void函数中使用.你怎么能早点从这样的功能回来?

在您的情况下,语句是冗余的,因为它已在所有控制路径上达到,但编译器肯定不会在该实例中发出诊断.

(请注意,必须 return显式来自非void函数,但main()编译器必须生成的情况除外return 0;.