[top-posting fixed]
Chris said:
Your code has undefined behavior. You might as well use [a
program with "short main[] = { ... };"].
I have no clue why you mentioned that (your solution)
for the OPs question !
Are you demonstrating an UB ?
Yes. The effect of undefined behavior is, well, undefined behavior.
The code I posted really does work ... on *some* systems. Similarly,
C code with undefined behavior -- like calling fclose(stdout) and
then calling printf() -- works on *some* systems, and fails on some.
Please elaborate on your program
The data inside the main[] array constitute machine instructions:
load up some registers, and make two system calls (SYS_write and
SYS_exit respectively). This particular operating system and
compiler pair do not place too great a distinction between code
and data, so as long as the main[] array is correctly aligned, it
works. (Making it "int main[]", and changing the data values,
would help avoid potential alignment problems.)
Again, the point is that "undefined behavior" does *not* mean "code
does not work". It also does *not* mean "code works OK". What
it means is "the C standard no longer tells you what the code
does." You must therefore look outside the standard.
Strictly conforming "standard C" code *always* works on *every*
"standard C" implementation (subject of course to things like
running out of memory in malloc(), for instance). Other code
*sometimes* works on *some* implementations. Which is better?
Well, that is a bit like asking whether a sailboat is better than
a motorboat -- it depends on whether you have fuel available and
need to get somewhere fast, too. There are tradeoffs in everything:
nonstandard code may achieve something that *cannot* be done with
standard code -- in which case, you have to use it -- or it might
run much faster, or be much easier to write. Or it may simply fail
to work in the places where it is needed, so that the standard code
would have been better, despite being longer or slower or harder.