Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: C++ forcing unnecessary changes to C? Date: Fri, 25 Nov 2016 12:30:00 -0800 Organization: None to speak of Lines: 44 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="3c5e60c3a5e134696d7a5f0d024ade78"; logging-data="28230"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Tidxwll4iOUU/90e1HaUX" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:kCsP8vATS2JmxizHh1psqFnpej8= sha1:aPneiJ8wUENZ/ZNao6jOJbwGVXs= Xref: csiph.com comp.lang.c:97478 Robert Wessel writes: > On Tue, 22 Nov 2016 11:11:52 +0100, jacobnavia > wrote: >>Le 21/11/2016 à 10:26, Robert Wessel a écrit : >>> Falling off the end of main() without a return was legal in C89, but >>> the actual value returned to the environment was undefined. >> >>Any sensible compiler would complain since returning from a function >>that should return a value without assigning the return value is always >>an error. > > No, the point is that C89 makes a special dispensation (5.1.2.2.3) for > main() (only in a hosted environment, and for no other function), so > that falling off the end of main() without a return is *not* an error. > At least from a standard's perspective. It's darn bad practice, of > course. It depends on what you mean by "an error". It is neither a constraint violation nor undefined behavior. C89 had no section 5.1.2.2.3; that's from the equivalent but renumbered ISO C90 standard. (It would have been 2.1.2.2.3 in the ANSI C89 standard.) What that section says is that If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined. 6.6.6.4 says that reaching the closing "}" is equivalent to a "return;". Falling off the end of *any* function without returning a value is not an error in C89/C90. It's only attempting to use the result that has undefined behavior. C99 and C11 keep that rule; a "return;" with no value is forbidden in a non-void function, but it's still legal (but a bad idea) to fall off the end. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"