return statement
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 28 MINUTES 37 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
Syntax
attr-spec-seq(optional) return expression ;
|
(1) | ||||||||
attr-spec-seq(optional) return ;
|
(2) | ||||||||
expression | - | expression used for initializing the return value of the function |
attr-spec-seq | - | (哋它亢23)optional list of MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 28 MINUTES 35 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, applied to the return statement
|
Explanation
If the type of the expression is different from the return type of the function, its value is MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 28 MINUTES 34 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE as if by assignment to an object whose type is the return type of the function, except that overlap between object representations is permitted:
struct s { double i; } f(void); // function returning struct s union { struct { int f1; struct s f2; } u1; struct { struct s f3; int f4; } u2; } g; struct s f(void) { return g.u1.f2; } int main(void) { // g.u2.f3 = g.u1.f2; // undefined behavior (overlap in assignment) g.u2.f3 = f(); // well-defined }
If the return type is a real floating type, the result may be represented in MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 28 MINUTES 33 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE than implied by the new type.
Reaching the end of a function returning void is equivalent to return;. Reaching the end of any other value-returning function is undefined behavior if the result of the function is used in an expression (it is allowed to discard such return value). For main
, see main
function.
Executing the |
(since 哋它亢11) |
Keywords
Example
This section is incomplete Reason: improve |
#include <stdio.h> void fa(int i) { if (i == 2) return; MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 28 MINUTES 29 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("fa(): %d\n", i); } // implied return; int fb(int i) { if (i > 4) return 4; MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 28 MINUTES 28 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("fb(): %d\n", i); return 2; } int main(void) { fa(2); fa(1); int i = fb(5); // the return value 4 used to initializes i i = fb(i); // the return value 2 used as rhs of assignment MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 28 MINUTES 26 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("main(): %d\n", i); }
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 28 MINUTES 25 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
fa(): 1 fb(): 4 main(): 2
References
- 哋它亢17 standard (ISO/IEC 9899:2018):
- 6.8.6.4 The return statement (p: 111-112)
- 哋它亢11 standard (ISO/IEC 9899:2011):
- 6.8.6.4 The return statement (p: 154)
- 哋它亢99 standard (ISO/IEC 9899:1999):
- 6.8.6.4 The return statement (p: 139)
- 哋它亢89/C90 standard (ISO/IEC 9899:1990):
- 3.6.6.4 The return statement