71

This compiles without any warnings.

Is this legal in C and C++ or does it just work in gcc and clang?

If it is legal, is it some new thing after C99?

void f(){}

void f2(){
    return f();
}

Update

as "Rad Lexus" suggested I tried this:

$ gcc -Wall -Wpedantic -c x.c 
x.c: In function ‘f2’:
x.c:7:9: warning: ISO C forbids ‘return’ with expression, in function returning void [-Wpedantic]
  return f();

$ clang -Wall -Wpedantic -c x.c 
x.c:7:2: warning: void function 'f2' should not return void expression [-Wpedantic]
        return f();
        ^      ~~~~~
1 warning generated.

$ gcc -Wall -Wpedantic -c x.cc
(no errors)

$ clang -Wall -Wpedantic -c x.cc
(no errors)

Update

Someone asked how this construction is helping. Well is more or less syntactic sugar. Here is one good example:

void error_report(const char *s){
    printf("Error %s\n", s);
    exit(0);
}

void process(){
   if (step1() == 0)
      return error_report("Step 1");

   switch(step2()){
   case 0: return error_report("Step 2 - No Memory");
   case 1: return error_report("Step 2 - Internal Error");
   }

   printf("Processing Done!\n");
}
16
  • 9
    Voted to re-open; proposed duplicate was for C++ only. This is also tagged C. (C and C++ differ considerably in their use of void).
    – Bathsheba
    Mar 14, 2016 at 12:34
  • 16
    So you're asking for C or C++? Pick a language.
    – 2501
    Mar 14, 2016 at 12:37
  • 8
    Note: with both gcc -Wall -Wpedantic -std=c99 and -std=c11, you get a warning: "warning: ISO C forbids 'return' with expression, in function returning void [-Wpedantic]".
    – Jongware
    Mar 14, 2016 at 12:39
  • 11
    @HolyBlackCat: Can I ask about C, C++ and Java at the same time as well? ;-)
    – DevSolar
    Mar 14, 2016 at 12:40
  • 7
    @DevSolar :D Well, I'll state that more clear. IMO it's legit to ask about a feature that exists in both C and C++. Usually those features behave similarly in both languages. If not, a good answer would describe the diffefence. Mar 14, 2016 at 12:43

5 Answers 5

77

C11, 6.8.6.4 "The return statement":

A return statement with an expression shall not appear in a function whose return type is void.

No, you may not use an expression, even if it is of void type.

From the foreword of the same document:

Major changes in the second edition included:

[...]

  • return without expression not permitted in function that returns a value (and vice versa)

So this was a change from C89 -> C99 (the second edition of the language standard), and has been that way ever since.


C++14, 6.6.3 "The return statement":

A return statement with an expression of non-void type can be used only in functions returning a value [...] A return statement with an expression of type void can be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its caller.

Yes, you may use an expression if it is of void type (that's been valid since C++98).

16
  • 1
    "A return statement with an expression of type void can be used only in functions with a return type of cv void" Why would anyone want to write such nonsense code though? Rather than just writing void f2(){ f(); }. Seems like there must be a rationale for it somewhere, even though it is C++?
    – Lundin
    Mar 14, 2016 at 13:06
  • 7
    @Lundin: Templates? I assume. Looks like <= C99 allowed it as well, and C++ probably just copied that behaviour.
    – DevSolar
    Mar 14, 2016 at 13:06
  • 10
    @Lundin: In C++, mostly for templates. If the return type of f and f2 are arguments to the template, being able to write return f(); without having to special case void is handy. (obviously this particular sample code doesn't really work as a template - but with more complex code it can be useful - see the futures stuff for example. Mar 14, 2016 at 16:45
  • 3
    As Mohit's answer correctly quotes, the item "return without expression not permitted in function that returns a value" is under Foreward paragraph 7 "Major changes in the second edition" not paragraph 6 "This third edition ... [m]ajor changes". Second edition was C99, which changed C89/90 only for return without expression; return with expression in a void function was prohibited since C89/90, when void was first introduced. @Lundin before C99 return without expression in a non-void function and the caller trying to use the value was UB. Mar 14, 2016 at 18:45
  • 2
    In particular, in the case where the function is executed synchronously, the member will just return external_function(args...); Mar 15, 2016 at 9:02
16

This code is allowed in C++ but not allowed in C

From Return statement @ cppreference

In a function returning void, the return statement with expression can be used, if the expression type is void.


OTOH in C11 specs draft n1570:

Major changes in the second edition included:

return without expression not permitted in function that returns a value (and vice versa)

(return with expression not permitted in function that returns a void)

and 6.8.6.4 return

A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

(even if the expression evaluates to void)

5
  • It would be better to quote the C++ standard and possibly document since which version the construction is allowed. cppreference is not a very dependable source, many code samples have bugs.
    – chqrlie
    Mar 14, 2016 at 12:49
  • @chqrlie Can you give me a link to a page where code sample is wring @ en.cppreference?
    – Mohit Jain
    Mar 14, 2016 at 13:16
  • @MohitJain: just an example: en.cppreference.com/w/c/string/byte/strncat compiled with gcc 4.6: main.cpp:31:1: warning: control reaches end of non-void function [-Wreturn-type]. Not an error since C99, but bad style.
    – chqrlie
    Mar 14, 2016 at 14:03
  • @chqrlie Thanks. I don't have C++ specs, I use latest drafts and en.cppreference for my reference whenever I need it.
    – Mohit Jain
    Mar 14, 2016 at 16:05
  • 1
    @MohitJain: The C++ latest drafts are close enough. The source code is actually available on github! I wish the C and C++ Standards were publicly available for free. cppreference is actually very well documented, my earlier judgement may have reflected my impressions based on the French translation. Any visible bugs can be fixed as they are spotted.
    – chqrlie
    Mar 14, 2016 at 16:41
5

C++ allows something like that:

void f() 
{
    return void();
}

While C does not. That's why a warning is issued if you compile it a ISO C rather than ISO C++. This is formally described as:

A return statement with an expression of type void can be used only in functions with a return type of cv void

1
  • 1
    It looks correct, but standard references for both claims would be nice. Mar 14, 2016 at 12:46
3

ISO/IEC 9899:201x Committee draft says the following:

6.8.6.4 The return statement

Constraints

  1. return statement with an expression shall not appear in a function whose return type is void.

    A return statement without an expression shall only appear in a function whose return type is void.

So, it is forbidden in C.


You need to use -pedantic switch to gcc for it to complain about standard violations:

test.c: In function ‘f2’:
test.c:6:12: warning: ISO C forbids ‘return’ with expression, in function returning void 
            [-Wpedantic]
     return f();
1

Standard C does not support this construction:

C11 6.8.6.4: The return statement

Constraints

1 A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

No special provisions are added for the special case in the question. Some C compilers do support this as an extension (gcc does, unless instructed to conform to one of the C Standards), but C11 and previous versions consider it a constraint violation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.