Which of the above is correct according to the standard?
Neither is correct according to C and C++ standard. C standard says that:
6.8.6.4 The return statement:
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
.
Also read this answer for more detailed explanation.
Second, why does it compile?
Compilation doesn't guarantee that your code can't produce errors. It can. Better to compile your code with maximum warning flags. I compiled the second code with following options and got the following warnings:
In function 'main':
[Warning] implicit declaration of function 'a' [-Wimplicit-function-declaration]
At top level:
[Warning] conflicting types for 'a' [enabled by default]
[Note] previous implicit declaration of 'a' was here
In function 'a':
[Warning] 'return' with a value, in function returning void [enabled by default]
Although code compiled but there are some errors and violation of C standard.