This is not a desired behaviour, though not explicitly mentioned as undefined behaviour. This is a "Constraints violation".
Quoting C11
, chapter 6.8.6.4
[...] A return
statement without an expression shall only appear in a function
whose return type is void
.
and, from chapter §5.1.1.3, Diagnostics,
A conforming implementation shall produce at least one diagnostic message (identified in
an implementation-defined manner) if a preprocessing translation unit or translation unit
contains a violation of any syntax rule or constraint, even if the behavior is also explicitly
specified as undefined or implementation-defined. [....]
In this case, compiler proceeds to generate the executable code, so, yes, executing this binary invokes undefined behavior.
Bottom line, for main()
which is expected to return an int
should not have a return
statement without an expression. Refrain from writing code like that.
[Note: As per this discussion, there's a strong notion that this is undefined behaviour, but still, there's no explicit mention in the standard.]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…