Before anyone may mark it duplicate of related questions. I emphasize I DO have read all those questions. But I still have some interrogations(yep, some little pedantic 🙂 )
For C
Some conclusions:
1. In C89(C90), this is _undefined_ .
2. In C99(or C11), a type of int is madatory; control flow reached the closing }
will return a value of 0.
Here comes my interrogations.
-
In c89, I have found nothing about undefined, but unspecified?
Detail: The related parts in C89 are 5.1.2.2.1 Program startup and 5.1.2.2.3 Program termination (NOTE : both are under the 5.1.2.2 Hosted environment section, within which our later discussion is limitted)
Cite: — 5.1.2.2.3 Program termination —
A return from the initial call to the main function is
equivalent to calling the exit function with the value
returned by the main function as its argument.10 If the }
that terminates the main function is reached, the
termination status returned to the host environment is
unspecified.Just note that part: If the } that terminates … , it clearly says
that if we omit the return type – thus the } will be reached at –
the termination status is unspecifiedAccording the definition of the standard of undefined and unspecified,
Should I say that it gives unspecified value since whatever it return is a
legal int value, but the consequese is undefined-we could not predict what value
will lead to what catastrophic consequese? -
In c99, a type of int is madatory, but
gcc --std=c99given a test without int type(no return type actually) gives only waring:return type of ‘main’ is not ‘int’ ,but not error ?Detail: the related parts are the same as that in c89.
Cite: — 5.1.2.2.1 Program startup —
It shall be defined with a return type of int and ...and — 4. Conformance —
1. In this International Standard, ‘‘shall’’ is to be interpreted as a requirement on an implementation or on a program; conversely, ‘‘shall not’’ is to be interpreted as a
prohibition.So shall should be interpreted as madatory in this standard, why gcc with swith –std=c99 violated this?
C89/90 still has the implicit int rule, so
main()is equivalent toint main(). By leaving off the return type, you’ve implicitly defined the return type asint. Some may consider this sloppy, but it’s strictly conforming (i.e., involves no implementation defined, undefined or unspecified behavior).For C99, the implicit
intrule has been removed, somain()isn’t defined. However, a compiler is only required to “issue a diagnostic” upon encountering a violation of aShallorShall notclause — it’s still free to continue compiling after issuing the diagnostic. Exactly what constitutes a diagnostic is implementation defined. As such, all that’s needed for gcc to conform in this respect is documentation to say that the warning it issues is to be considered a diagnostic.Edit: “implicit
int” in the C89/90 standard isn’t really a single rule in a single place — it’s spread around in a couple of places. The primary one is §6.5.2.1, where it says:This is part of a list, where all the items on each line of the list are considered equivalent, so this is saying that (unless otherwise prohibited) lack of a type specifier is equivalent to specifying (signed)
int.For function parameters, there’s a separate specification (at §6.7.1) that: “Any parameter that is not declared has type
int.”