0

I have tried basic implementation of atexit(). Although return type of main() is int, compiler gives no error when executed without returning any value from main.

#include <stdio.h>
#include <stdlib.h>
void my_exit1(void);
void my_exit2(void);

int main(void)
{
    if (atexit(my_exit2) != 0)
        printf("can't register my_exit2");

    if (atexit(my_exit1) != 0)
        printf("can't register my_exit1");

    printf("starting main\n");
    printf("main is done\n");
    //return(0);
    //exit(0);
}

void my_exit1(void)
{
    printf("first exit handler\n");
}

void my_exit2(void)
{
   printf("second exit handler\n");
}

OUTPUT:

starting main
main is done
first exit handler
second exit handler
Amogh Mishra
  • 1,088
  • 1
  • 16
  • 25
  • 1
    Possible duplicate of [where does main() return its value?](http://stackoverflow.com/questions/2637671/where-does-main-return-its-value) – 2501 Feb 15 '16 at 08:57
  • It would not be a run-time error. It would be a compile-time warning. Try enabling warnings and see if you get one for `main` not returning anything. – Tom Karzes Feb 15 '16 at 08:58
  • Spoilers, it implicitly returns 0. – 2501 Feb 15 '16 at 08:58
  • 3
    Possible duplicate of http://stackoverflow.com/questions/22239/why-does-int-main-compile – Kai Wu Toh Feb 15 '16 at 08:58
  • @2501 No, there is no implicit return value. The return value is undefined. – Tom Karzes Feb 15 '16 at 09:01
  • 1
    @TomKarzes Read: http://stackoverflow.com/a/22262/4082723 – 2501 Feb 15 '16 at 09:04
  • 1
    @2501 You're confusing C++ with C. They are quite different in this regard. Trust me, in C the return value is undefined. In fact I tried it with gcc, and I happened to get a return value of 1 from `main` when I called it recursively. – Tom Karzes Feb 15 '16 at 09:11
  • 1
    @2501 the C++ standard specifies an implicit return value of 0 when main _falls off_, but according to the C standard, the behaviour is undefined. A lot of implementations however do return 0, like some implementations still provide the implicit return type `int`. But again: this is non-standard behaviour. – Elias Van Ootegem Feb 15 '16 at 09:13
  • 1
    @TomKarzes See below. I linked a C++ answer, but C also returns an implicit 0.Here is a C answer: https://stackoverflow.com/questions/13232784/why-does-main-not-need-a-return-statement/13232822#13232822 – 2501 Feb 15 '16 at 09:17
  • @EliasVanOotegem See above. In *C11, 5.1.2.2.3, p1* says that if main is int main you get an implicit return of 0. – 2501 Feb 15 '16 at 09:19
  • @2501 Ok, I tried my example again in gcc, this time specifying `--std=c99`, and that time `main` returned 0. Without specifying `c99` it didn't. So I guess in traditional C the result was undefined, but in `c99` it was forced to return 0. And apparently my gcc installation does not comply with c99 by default. – Tom Karzes Feb 15 '16 at 09:22
  • @TomKarzes Great. I always assume (and consider C99 if necessary) the latest standard C11(as should we) unless specified otherwise. – 2501 Feb 15 '16 at 09:23
  • @2501 This is interesting: [C99 not default](http://stackoverflow.com/questions/5060799/c99-not-default-c-version-for-gcc) – Tom Karzes Feb 15 '16 at 09:25
  • @EliasVanOotegem I agree, with the last sentence. Tom's arguments weren't on the topic though, and OP is clearly using >= C99, so I didn't address them. – 2501 Feb 15 '16 at 09:37
  • @2501: Removed comment because I realized that I was at risk of drifting off-topic and being a bit too pedantic to contribute to a constructive argument. In the end, we all agree that there's no good reason not to have a return in the main function, and that the standard is where you turn to if you want to explain the behaviour the OP encountered. If C99 and up specify an implicit `0` return, that's all there is to know. If the OP were to expect an implicit `0`, but saw something else, he needs to check the compiler he's using (if gcc, specifify `-std=c11` or `c99` etc...) – Elias Van Ootegem Feb 15 '16 at 09:43

1 Answers1

0

In the current C version, int main() can be left without a return value at which point it defaults to returning 0. This is also true for a C99 program.

Whether return 0 should be omitted or not is open to debate. The range of valid C program main signatures is much greater.

2501
  • 25,460
  • 4
  • 47
  • 87
msc
  • 33,420
  • 29
  • 119
  • 214