简体   繁体   English

C中void函数中的return语句

[英]return statement in void function in C

In the following program, Why compiler does not gives any error or warning? 在以下程序中, 为什么编译器没有给出任何错误或警告?

cc -Wall -pedantic my_program.c

code here: 代码在这里:

#include <stdio.h>

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

int main()
{
        f();
        return 0;
}

I have compiled the program in GCC compiler on Linux platform. 我已经在Linux平台上的GCC编译器中编译了程序。

Of course you can use return; 当然可以使用return; in a void function. void函数中。 How else would you return early from such a function? 您将如何从这种功能中尽早返回?

In your case the statement is redundant as it's reached on all control paths, but a compiler will certainly not issue a diagnostic in that instance. 在您的情况下,该语句是多余的,因为它在所有控制路径上均已到达,但是编译器肯定不会在该实例中发出诊断。

(Note that you must return explicitly from a non- void function, with the exception of main() , where a compiler must generate return 0; if missing. (请注意,您必须从非void函数显式return ,但main()除外,在该函数中,编译器必须生成return 0;如果丢失。

A void function will perform the task, and then control returns back to the caller but, it's not going to return a value. void函数将执行该任务,然后控制权返回给调用者,但不会返回值。 There isn't any value to return. 没有任何价值可返回。

Without the return statement, control will return to the caller at the end of the function anyways. 如果没有return语句,控制将始终在函数结尾处返回到调用方。

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

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