Path: utzoo!mnetor!uunet!husc6!rutgers!dayton!viper!john From: john@viper.Lynx.MN.Org (John Stanley) Newsgroups: comp.lang.c Subject: Re: return vs exit() Message-ID: <454@viper.Lynx.MN.Org> Date: 26 Dec 87 03:39:13 GMT References: <10875@brl-adm.ARPA> <176@fxgrp.UUCP> <1286@laidbak.UUCP> <1293@laidbak.UUCP> <10033@shemp.UCLA.EDU> Reply-To: john@viper.UUCP (John Stanley) Organization: DynaSoft Systems Lines: 64 In article <10033@shemp.UCLA.EDU> alex@CS.UCLA.EDU (Alex Quilici) writes: > >A *return from main* and exit() are equivalent. In theory: maybe. In practice: not-on-a-bet....! This is a long standing illusion. For practical reasons, many implementors of the C language have had to make a concession to how people have been told (by K&R no less) to write a main() function. There are two ways for a call to main() to be written: 1) /* used in most of the crt0.c code that I've seen */ main(argc, argv); exit(0); 2) /* what Alex would expect */ exit(main(argv, argc)); The first version above will insure that a program that does not return anything from main has a zero exit code... This also means that any main that returns something will also have a zero exit code (not what Alex expects, but something that can be overcome by using an exit() call if the programmer desires to return something other than zero...). The second version insures that a value returned from main will be accepted as an exit code, but also insures a random return value for programs with a main that doesn't end with a return. I'm sure you'll all agree that a random return value isn't desireable? There are an abundance of existing programs that -do-NOT- return any value from main. They just terminate. There is no way of telling what the value handed back to the calling crt0() function will be. You may say these are poorly written, I'd tend to agree, but in this situation, poor style isn't really relevant...they exist and part of the objective for the ANS standard is to not break existing code... (Examples of this style are in K&R and must be considered part of the baseline definition of how many people have been writing main() functions for years.) What this all boils down to is this... If a programmer wants to return any value to the operating system and have the program work on a large number of existing implementations (that use version one -or- version two of the main() call I listed), he/she -must- use exit(). Any other method will not work on many existing systems... Assuming that returning a value from main will work is logical, but it just won't work... If the ANS standard does not deal with this problem, it should do so before being released. This dicotomy has existed for years and is exactly the kind of thing the committee needs to solve once and for all... >But you can use exit() to leave your program from any function within >it. If nothing else, exit() is convenient for fatal-error handlers. Regardless of which way the ANS standard is written, if you use exit to return a value out of your program, it will work. This makes exit more than just a convenience, it's a necessity for anyone who has to write code that needs to be portable. --- John Stanley (john@viper.UUCP) Software Consultant - DynaSoft Systems UUCP: ...{amdahl,ihnp4,rutgers}!meccts!viper!john