News for hackers. Stuff that matters.
Re:Main is usually a function, so when is it not? (2015) by user5994461 on Sunday September 10, 2017 @09:37AM (#15212168)
You have to return something. If you don't, the exit code is the value that happened to be in memory at this point.
Re:Main is usually a function, so when is it not? (2015) by clarry on Sunday September 10, 2017 @09:46AM (#15212196)
N1256 5.1.2.2.3 p1: If the return type of the main function is a type compatible with int, 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; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.
Re:Main is usually a function, so when is it not? (2015) by user5994461 on Sunday September 10, 2017 @09:57AM (#15212226)
main() is __cdecl__ or __stdcall__ on the major platforms.

That calling convention on x86 specifies that the return value is red from the EAX register. So, when your main() function exits, the return value is red from EAX, it's that simple.

The compilers may add boilerplate code around the main, in fact main() is rarely the real main() function, but that doesn't change the spec.

Re:Main is usually a function, so when is it not? (2015) by TazeTSchnitzel on Sunday September 10, 2017 @10:00AM (#15212241)
Did you read the comment you just replied to?!
Re:Main is usually a function, so when is it not? (2015) by pjc50 on Sunday September 10, 2017 @10:12AM (#15212282)
The two comments are not incompatible, they are just very different worldviews. One tells you what the standard says (that the termination status is unspecified if main does not return an int), the other tells you what usually happens (you get what happened to be in AX).

And as I've just tested, gcc doesn't return zero termination status if you reach the } at the end of main.

Re:Main is usually a function, so when is it not? (2015) by user5994461 on Sunday September 10, 2017 @10:32AM (#15212351)
Calling conventions are as much a standard as the C spec.

The main() is called like a regular function, by the system thing that executes programs.

Depending on the compiler and the flags, the main() is not the real entry point of the program. It can add another entry point to do some magic, like setting the return code.

Re:Main is usually a function, so when is it not? (2015) by TazeTSchnitzel on Sunday September 10, 2017 @10:21AM (#15212314)
Depends if it's in C99 mode. GCC defaults to “GNU89” I think.
Re:Main is usually a function, so when is it not? (2015) by TazeTSchnitzel on Sunday September 10, 2017 @09:44AM (#15212186)
Normally, yes. But main() is special and implicitly returns 0, for whatever reason.
Re:Main is usually a function, so when is it not? (2015) by user5994461 on Sunday September 10, 2017 @09:51AM (#15212207)
Absolutely not. You can look at a crashing programs and see the return code is a random value. You can also make a program that doesn't return anything and see that it's also a random value, though some compilers on some platforms might initialize that to zero.
Re:Main is usually a function, so when is it not? (2015) by TazeTSchnitzel on Sunday September 10, 2017 @10:00AM (#15212236)
No, it's literally special-cased in the C standard.
Re:Main is usually a function, so when is it not? (2015) by pjc50 on Sunday September 10, 2017 @10:15AM (#15212293)
Someone should tell the gcc maintainers then.

    int main()
    {
        int a = 12;
    }

    $ gcc maintest.c
    $ ./a.out
    $ echo $?
    148
Re:Main is usually a function, so when is it not? (2015) by TazeTSchnitzel on Sunday September 10, 2017 @10:18AM (#15212297)

  $ cat test.c
  int main(void) {int a = 12;}
  $ gcc-6 test.c -o test --std=c89; ./test; echo $?
  162
  $ gcc-6 test.c -o test --std=c99; ./test; echo $?
  0
Must be a C99 thing.
Re:Main is usually a function, so when is it not? (2015) by albinofrenchy on Sunday September 10, 2017 @12:01PM (#15212757)
This isn't that conclusive of a test -- uninitialized memory is 0 relatively often.
Re:Main is usually a function, so when is it not? (2015) by clarry on Sunday September 10, 2017 @01:55PM (#15213356)
Re:Main is usually a function, so when is it not? (2015) by pjscott on Sunday September 10, 2017 @06:00PM (#15214517)
Okay, that is conclusive. (And I'm pleasantly surprised by how readable GCC's source code is.)
Re:Main is usually a function, so when is it not? (2015) by kaushiks on Sunday September 10, 2017 @04:12PM (#15214093)
This has nothing to do with memory. The x86_64 ABI returns values from functions in the RAX register.
Re:Main is usually a function, so when is it not? (2015) by albinofrenchy on Sunday September 10, 2017 @04:45PM (#15214243)
Sure; same point applies though -- just because 0 happens to be in RAX doesn't mean it's defined behavior.
Re:Main is usually a function, so when is it not? (2015) by user5994461 on Sunday September 10, 2017 @10:15AM (#15212294)
Just tested on the MSVC compiler I have at hands. The standard is not respected.
Re:Main is usually a function, so when is it not? (2015) by detaro on Sunday September 10, 2017 @10:20AM (#15212309)
MSVC doesn't claim to implement C99, where this rule was added, so it kind of makes sense that it doesn't happen. (generally, if they added C99-stuff then only where it was required for C++)
Re:Main is usually a function, so when is it not? (2015) by user5994461 on Sunday September 10, 2017 @10:42AM (#15212395)

    int main(void)
    {
        __asm {
          mov eax, 123
        }
    }

    // visual studio command line
    // cl testc.c
    // cl testcpp.cpp
    // testc.exe
    // echo C %errorlevel%
    // testcpp.exe
    // echo CPP %errorlevel%
Indeed. It's a C99 thing.

For those not familiar with MSVC. The C compiler is not C99 compliant and the C++ compiler is.

Re:Main is usually a function, so when is it not? (2015) by 1_2__4 on Sunday September 10, 2017 @11:32AM (#15212624)
I love C but this whole thread highlights a lot of the criticism of it. Everyone's right based on some standard or switch, and everyone's wrong for the same reason.
Re:Main is usually a function, so when is it not? (2015) by jstimpfle on Sunday September 10, 2017 @01:51PM (#15213333)
This is not specific to C, but common to all evolving standards. If you want portable code just drive on the middle of the road.

Often this is as easy as coding to an older standard, like C89 with a few selected features from later standards, and adding some compatibility features / problem detection in the build system.

And don't rely on features that are difficult to explain and/or add only questionable value. Just return 0 from main, it's the obvious simple thing to do.