Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

return 0 vs. return 1 in main()?

Ed HallEd Hall Posts: 395Member
I've been under the impression that main should always return an integer, and further that if the program operated correctly, it should return 0. I see a lot of programs which use return 1 at the end of main(). Isn't this telling the calling program that an error was detected when the called program finished? I realize that if the calling program doesn't check for errors, the 0 vs. 1 would probably be moot, but shouldn't 0 be used, just in case?

Along the same lines, there are a lot of tutorials/info sites that don't have any return in their main() sections, and many without main() declared as an integer. Are compilers just being built that don't care about these items, and if so, should programmers? Thanks.

Take Care,
Ed

«1

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-2-24 10:28:25[/red][/b][hr]
    : I've been under the impression that main should always return an integer, and further that if the program operated correctly, it should return 0. I see a lot of programs which use return 1 at the end of main(). Isn't this telling the calling program that an error was detected when the called program finished? I realize that if the calling program doesn't check for errors, the 0 vs. 1 would probably be moot, but shouldn't 0 be used, just in case?

    [blue]traditionally, program should return 0 as you indicated. But there's nothing really wrong with returning something else -- can even return a pointer if you want, but if you do the compiler will convert the pointer value into an int. The calling program is responsible for interpreting the return value, not the other way around. The program returning the value has no such obligation.[/blue]
    :
    : Along the same lines, there are a lot of tutorials/info sites that don't have any return in their main() sections, and many without main() declared as an integer. Are compilers just being built that don't care about these items, and if so, should programmers? Thanks.
    :

    [blue]Most compilers return 0 if not otherwise specified. And void main() is legal in C, but not in C++.[/blue]

    :





  • kooladikooladi Posts: 72Member
    hi..well what can i say ..
    many books are written by people who don't even bother to actually learn their subject. They are good at writing books and think they know everything. Some of them test their code by compiling and running it with their favorite compiler, and never notice that they are using compiler specific extensions. Some authors have written books containing code which they just wrote off the top of their head. They never compiled and ran it because it would not compile with ANY compiler.Sadly, many instructors of computer programming classes do not know the actual standard for the language themselves. What they know they learned from reading the same books written by careless authors described above. This kind of ignorance of their subject matter would not be allowed in a subject like mathematics, chemistry, or physics, but it is all too common in computer programming.Microsoft is particularly bad at this. Almost all of the examples in their online help start with void main(void). Yet if you look up main in the online help, it displays a proper definition. The example programs in the online help for Borland compilers always show main() properly returning an int.
    ANSI specifes that main should always be declared as returning int..and not void..Infact any strict ANSI compliant complier will flag it as an error like gcc does.
    As far as programs returning 1 from main..well what can i say except that they dont know whats the return value is being used for and do it bcoz they saw somebody else do it..or maybe there complier was complaining and just to shut it up..they start returning 1 . As u said, if no comparsion is taking place at the OS end..its just moot..so there programs run and they make it a habit.
    You have a good habit..pls dont break it.
    I hope i was of any help
    Programming is a race between software engineers striving to build bigger and better idiot-proof programs,and the Universe trying to produce bigger and better idiots. So far,the Universe is winning.

  • stoberstober Posts: 9,765Member ✭✭✭

    : ANSI specifes that main should always be declared as returning int..and not void..Infact any strict ANSI compliant complier will flag it as an error like gcc does.


    not correct
    http://homepages.tesco.net/~J.deBoynePollard/FGA/legality-of-void-main.html
  • LundinLundin Posts: 3,711Member
    :
    : : ANSI specifes that main should always be declared as returning int..and not void..Infact any strict ANSI compliant complier will flag it as an error like gcc does.
    :
    :
    : not correct
    : http://homepages.tesco.net/~J.deBoynePollard/FGA/legality-of-void-main.html
    :


    Here we go again. This topic comes up every month or so :-)
    We know that the standard is vauge, and you can ready it in what way you perfer.

    The fact is that programs where main is not returning int:
    -won't work in gcc
    -won't be compatible with C++

    So there is no reason why you should write anything else. What the standard says or not doesn't really matter in this case, sence the majority of the programmers prefer to read the standard as "int is only allowed type".

    And there is also the problem with "calling convention". The OS must know the format of main to be able to call it.
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-2-24 12:3:7[/red][/b][hr]
    : The fact is that programs where main is not returning int:
    : -won't work in gcc [red]<< not relevant. That compiler does not set the standards. If it's a C compiler and doesn't accept void main, then it is not compliant with the C standard.[/red]

    : -won't be compatible with C++ [red]<< void main not allowed here anyway.[/red]
    :
    : So there is no reason why you should write anything else.
    [red]Sure there is -- I might be too lazy to type int main() and return 0 at the end of the function :-)[/red]

    : What the standard says or not doesn't really matter in this case, sence the majority of the programmers prefer to read the standard as "int is only allowed type". [red]Agree -- BUT the C standard says it can be anything else. There is a lot of C code that uses void main() construct.[/red]
    :
    : And there is also the problem with "calling convention". The OS must know the format of main to be able to call it.[red]No it doesn't. The OS does NOT call main(), the program's startup code does. main() can be declared with or without parameters. The program's startup code creates the parameters, pushes argc, argv and (M$ VC++) env on the stack, calls main(), then pops them off the stack when main() returns. this is all done from inside the program itself, not from the os. [/red]
    :













  • Ed HallEd Hall Posts: 395Member
    : not correct
    : http://homepages.tesco.net/~J.deBoynePollard/FGA/legality-of-void-main.html
    :
    Thanks for the link. A very interesting article. I would think that the "loophole" really couldn't be closed at this point. Any attempt to restrict C to int main() would cause too great a confusion in the programming sector. There are just too many sources of examples and teachings that wouldn't work unless the casual student was made aware of the change. All kinds of new or informal programmers, like myself, would be left wondering why samples and examples aren't compiling, although compilers could be capable of specifying that error.

    My understanding at this point is that C++ requires int main() while C recommends it. Is that basically correct? Thanks again eveyone.

    Take Care,
    Ed

  • dennisparkerdennisparker Posts: 478Member
    : My understanding at this point is that C++ requires int main() while C recommends it. Is that basically correct? Thanks again eveyone.
    :
    : Take Care,
    : Ed

    We went round with this not long ago. Both C and C++ standard specify that main return type int, but for C the result is unspecified if you don't.

    Sort of a point of trivia, kinda fun to argue but not muach practical use. Kind of like the C++ standard does require that C++ programs define a function main()(it is "implementation defined). A point of trivia, but as a practical matter kind of useless. :-)


  • DB1DB1 Posts: 1,142Member
    :
    : : ANSI specifes that main should always be declared as returning int..and not void..Infact any strict ANSI compliant complier will flag it as an error like gcc does.
    :
    :
    : not correct
    : http://homepages.tesco.net/~J.deBoynePollard/FGA/legality-of-void-main.html
    :


    foo.c compiled with gcc, __STRICT_ANSI__, using void main() with no return

    gcc -pedantic -errors -Wall -Werror -ansi -c foo.c -o foo.o
    foo.c:49: warning: return type of `main' is not `int'


    It seems ansi compliant to me, although I wouldn't use it :)

    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • LundinLundin Posts: 3,711Member
    : [b][red]This message was edited by stober at 2005-2-24 12:3:7[/red][/b][hr]
    : : The fact is that programs where main is not returning int:
    : : -won't work in gcc [red]<< not relevant. That compiler does not set the standards. If it's a C compiler and doesn't accept void main, then it is not compliant with the C standard.[/red]

    [blue]
    I agree that it doesn't set the standard, but it is the most conforming compiler out there, which means the chance is larger that it compiles according to ANSI C, than for example MS compilers.
    [/blue]

    : : -won't be compatible with C++ [red]<< void main not allowed here anyway.[/red]

    [blue]Yes, that was my point...[/blue]

    : :
    : : So there is no reason why you should write anything else.
    : [red]Sure there is -- I might be too lazy to type int main() and return 0 at the end of the function :-)[/red]

    [blue]
    If you just type main(), it means that you implicit will return an integer, so it is the same thing and allowed in the standard. [/blue]

    :
    : : What the standard says or not doesn't really matter in this case, sence the majority of the programmers prefer to read the standard as "int is only allowed type". [red]Agree -- BUT the C standard says it can be anything else. There is a lot of C code that uses void main() construct.[/red]
    : :
    : : And there is also the problem with "calling convention". The OS must know the format of main to be able to call it.[red]No it doesn't. The OS does NOT call main(), the program's startup code does. main() can be declared with or without parameters. The program's startup code creates the parameters, pushes argc, argv and (M$ VC++) env on the stack, calls main(), then pops them off the stack when main() returns. this is all done from inside the program itself, not from the os. [/red]

    [blue] And who checks the return value? Hardly the program itself.
    Anyway, you describe how it's done in Windows. It doesn't mean that it works that way on every OS, and neither does the C standard specify how the program should be called/executed from the OS.

    There is an interesting section in the standard called "portability issues". Here you can read:
    [italic]
    J.1 Unspecified behavior
    The following are unspecified:
    /--/
    -The termination status returned to the hosted environment if the return type of main is not compatible with int.
    /--/
    -A program in a hosted environment does not define a function named main using one of the specified forms
    [/italic]

    It is mentionad in the same undefined behaviour section as things like "An object is referred to outside of its lifetime", pretty basic behaviour in other words. Would you also say that refering to a local variable outside it's namespace is valid C?
    [/blue]
  • stoberstober Posts: 9,765Member ✭✭✭
    :
    [blue]: It is mentionad in the same undefined behaviour section as things like "An object is referred to outside of its lifetime", pretty basic behaviour in other words. Would you also say that refering to a local variable outside it's namespace is valid C?
    : [/blue]
    :


    There are quite a few references to that "unspecified behavior" thingy in the standards.
«1
Sign In or Register to comment.