hooverm@sysjj.mdcbbs.com (SQUID 6 on the DARK side) (03/11/91)
This is my first posting to this group ( I read the FAQ first!!). My question is basic and regarding the function main(). When a program is compiled/linked and there is no value returned to main, certain platforms will return a warning message stating that this function doesn't return a value. I thought that by declaring: void main(void) that I would get around this. Is this ANSI standard? If it is valid, why doesn't my SUN SparcStation like it? Is it possible to write code that will not kick out ANY warning messages when compiled, regardless of the platform or error message level?? I write for Borland (PC), UNIX (HP & SUN), as well as VMS (VAX) and this is giving me fits!! Please reply by E-Mail and I will summarize if there is sufficient interest. Mark <o===6 hooverm@mdcbbs.com
dave@cs.arizona.edu (Dave P. Schaumann) (03/12/91)
In article <1991Mar11.085439.1@sysjj.mdcbbs.com> hooverm@sysjj.mdcbbs.com (SQUID 6 on the DARK side) writes: >When a program is compiled/linked and there is no value returned to main, >certain platforms will return a warning message stating that this function >doesn't return a value. > >I thought that by declaring: void main(void) that I would get around this. > >Is this ANSI standard? If it is valid, why doesn't my SUN SparcStation like >it? [...] Well, first you have to understant that one of the oddities of C (IMHO) is that if you declare a function like this: main() { ... } The return type becomes "int" by implication. This is probably not the best idea in the world, but the backward-compatability crowd wouldn't have it any other way. Declaring main as a void is *not* a good idea. Many systems take the value returned by main() to be the "exit status" of the program. (Usually, a value of 0 means all is well, and a non-0 value means some kind of error has occurred). If you don't really care about this, just put a return 0 ; (or exit(0)), and all should be well. Of course, you should see another thread appearing in this group for a discussion about return values under VMS. -- Dave Schaumann | dave@cs.arizona.edu | Short .sig's rule!
meranda@iguana.cis.ohio-state.edu (deron meranda) (03/12/91)
In article <1135@caslon.cs.arizona.edu> dave@cs.arizona.edu (Dave P. Schaumann) writes: > In article <1991Mar11.085439.1@sysjj.mdcbbs.com> hooverm@sysjj.mdcbbs.com (SQUID 6 on the DARK side) writes: > > When a program is compiled/linked and there is no value returned to main, > > certain platforms will return a warning message stating that this function > > doesn't return a value. > > > > I thought that by declaring: void main(void) that I would get around this. > > > > Is this ANSI standard? [...] > > Well, first you have to understant that one of the oddities of C (IMHO) is > that if you declare a function like this: > > main() { ... } > > The return type becomes "int" by implication. This is probably not the best > idea in the world, but the backward-compatability crowd wouldn't have it > any other way. The "implied int" kinda reminds me of FORTRAN, yuck ! > Declaring main as a void is *not* a good idea. Many systems take the value > returned by main() to be the "exit status" of the program. (Usually, a > value of 0 means all is well, and a non-0 value means some kind of error > has occurred). If you don't really care about this, just put a return 0 ; > (or exit(0)), and all should be well. [...] Anyway, to be ANSI conformant, main must be declared as one of: int main( void ) or int main( int argc, char *argv[] ) Declaring main as "returning" void is not standard. Furthermore, if main returns without returning a value back, the results are undefined. Any platform is permitted to allow this "no return value" as an extension, but in general it should be avoided. The function main is confusing at first, because unlike most other functions, it has TWO legal declarations! As for returning a harmless value, you should #include <stdlib.h> and use either return(EXIT_SUCCESS) or exit(EXIT_SUCCESS) from main. Both the return and exit methods are equivalent. It is true that in most systems, EXIT_SUCCESS is defined as 0, but not all! Whenever you get a compiler warning about no return value, or return value type mismatches, in most cases it is probably safer to try to correct the problem by supplying a correct return value, rather than trying to redefine the declaration of the function itself :) Deron E. Meranda ( meranda@cis.ohio-state.edu )
gwyn@smoke.brl.mil (Doug Gwyn) (03/13/91)
In article <1991Mar11.085439.1@sysjj.mdcbbs.com> hooverm@sysjj.mdcbbs.com (SQUID 6 on the DARK side) writes: >Is this ANSI standard? No, there are only two strictly conforming interface definitions your program can use, int main(void) and int main(int argc, char **argv). You should always terminate by invoking the exit function or by returning a value from the main function. The return value zero is appropriate when you are not trying to report an error to the environment.