Thanks to All who replied: Casper Dik <casper@holland.sun.com> "Thomas M. Payerle" <payerle@physics.umd.edu> "Denes, Daniel \(BG-IT 51\)" <daniel.denes@bankgesellschaft.de> "Bill R. Williams" <brw@etsu.edu> DAUBIGNE Sebastien - BOR <sebastien.daubigne@atosorigin.com> "Meder Kydyraliev" <meder@areopag.net> "Keith Smith" <keith@smith.net> "Rafael Marcelino Koike" <r.koike@terra.com.br> McEnroe <mcenroe@dli.ernet.in> Rich Teer <rich.teer@rite-group.com> The consensus is that the use of void on main is wrong (The book is wrong), The use of "void main(int argc, char *argv[])" is not part of the Standard and the fuction prototype should return and int. See answers below: Casper Dik <casper@holland.sun.com> >Solaris 2.8 >GCC 2.95.3 "SMCgcc" >jedit, ddd, gdb > >In trying to compile some old test Unix C programs... I was not >able to compile the programs. After a while of tying to debug >the programs that were of format: > >void main(int argc, char *argv[]) >or > >void >main(int argc, char *argv[]) That's not legal according to the ANSI C standard; it also resulted in bad exit code if main() falls of it end. >failed to compile with errors on main: > ># gcc -pg prog03.02.c -o prog032 >prog03.02.c: In function `main': >prog03.02.c:15: warning: return type of `main' is not `int' > >The program compiled fine by removing "void" from the function >main. Why do those old UNIX programs used void ... it must have >been valid at some point ... there are quite a lot of such >programs in Unix books ei Practical Unix programing, Rago Unix >System Programming, Oreilly Unix System Programming etc. You should replace "void" with "int" to be forward compatible with C99. The programs are wrong; there are only two acceptable forms of main: int main(void) int main(int argc, char *argv[]) >What was the use of "void" as originally intended (granted it >seems not work now days). Did the language evolved (I tried >-ansi on compiling trying with the same type of errors). There >is a lot of usage of void in the Unix programming literature. It was intended solely because the programmers were lazy and wanted to shut lint up about missing return statements in main. >My question is in a nutshell can someone explain the usage where >it is ok to use void () and where it is not keeping in mind >there a quite a lot of function prototypes that use void ei: > >main(void) This is ANSI C for "main doesn't expect arguments" and returns (implictely) an int. >void handler(int) This is a function which does not return a valuie. >void main(int argc, char *argv[]) The function main does not return a value; however, that is not allowed by the standard. BTW, I think you should use a newer form of gcc. "Thomas M. Payerle" <payerle@physics.umd.edu> > What was the use of "void" as originally intended (granted it > seems not work now days). Did the language evolved (I tried > -ansi on compiling trying with the same type of errors). There > is a lot of usage of void in the Unix programming literature. Although not a professional c/c++ programmer, to my knowledge void is still very much a part of the c/c++ language. When used for a function return type, it means the function does not return anything (e.g. like the old Pascal distinction between subroutines (not returning anything) and functions (a subroutine with a return value). What has apparently changed is the strictness with which you can mess with the main subroutine. The main subroutine on Unix like systems was _ALWAYS_ supposed to return something (an error code, or 0 is successful). I believe older C compilers allowed you to declare main as void meaning that you were not going to bother to set the return code. This was likely just a special hack put in for main, and perhaps it allowed for some consistency across platforms; I do not claim to understand the historical reasoning. It might also be that older versions of c allowed the use of void as a return type to mean something along the lines of "I'm returning something, but I don't know what type yet". With recent emphasis on security, that is probably a bad usage and was eliminated. But on Unix, main() should always have an integer return type. I believe you are still able to avoid specifying the parameters to main as long as you do not use them, but not positive about that. void is still valid as a function return type, but only when the function actually does not return anything, and I believe will generate an error if the function actually gives an argument to the return command. Since main() must rturn something to the OS, void is not appropriate for main(). So most likely all you need to do is change void to int for the main() subs, and chances are if you used void elsewhere it should work. "Bill R. Williams" <brw@etsu.edu> The standard/legacy definition of a C program main() function is that it returns an 'int'. That's the way it WAS, and AFAIK that's still the way it IS. Whoever wrote those programs you are working on has violated the most basic level of writing a program in C. I have seen compiler/library definitions vary on what the argument list for main() is -- always 'int argc' and 'char *argv[]' or some equivalent variation -- and some allow more arguments beyond for accessing the environmental stack, etc. BUT in all cases 'main()' should ALWAYS return 'int'; that's where the operating system gets the "exit code" or "$?". NOTE: that some programs call 'exit()' rather than just (implicitly) 'return()', but you still should always declare 'int main(...)'. -- DAUBIGNE Sebastien - BOR <sebastien.daubigne@atosorigin.com> ANSI C requires main to return an int (status code). GCC defaults to Ansi C, so it warns you that the main prototype is not ANSI compliant. Note that it is just a Warning, not an error. Nevertheless, the program will still be compiled, so you can just ignore this message. Another solution is to change the compilation flag to disable Ansi and use another standard (see manpage). "Keith Smith" <keith@smith.net> "void handler(int)" or "void main(int argc, char *argv[])" is used to mean that the function does not return a value. The warning that gcc was giving u means that the function "main" must return a int. This is then used as the return status of the program. "void handler(vold)" is the same as "void handler()" "int main(void)" is the same as "int main()" () was used in K&R C as a function prototype so (void) was need to show a function that doesn't pass a parameter. Any new compiler will treat () and (void) as the same. More people, so I'm told, use () than (void). "Rafael Marcelino Koike" <r.koike@terra.com.br> All programs must return an error code. This is why you need to declare int main( int argc, char *argv[]) if you don't want to return the error/sucess code, just return 0 McEnroe <mcenroe@dli.ernet.in> as per the ANSI C standard default return type of a function is 'int' it is better to use int,...if u have any value to return...or if u dont have any value to return while a function fnish executing ..then u can use void. Rich Teer <rich.teer@rite-group.com> > void main(int argc, char *argv[]) > or > > void > main(int argc, char *argv[]) > > failed to compile with errors on main: > > # gcc -pg prog03.02.c -o prog032 > prog03.02.c: In function `main': > prog03.02.c:15: warning: return type of `main' is not `int' > > The program compiled fine by removing "void" from the function > main. Why do those old UNIX programs used void ... it must have > been valid at some point ... there are quite a lot of such No. void main () has NEVER been valid. > programs in Unix books ei Practical Unix programing, Rago Unix > System Programming, Oreilly Unix System Programming etc. Bad habits picked up by programmers using compilers that didn't flag that error. :-( > My question is in a nutshell can someone explain the usage where > it is ok to use void () and where it is not keeping in mind In a C program (as opposed to C++), it is NEVER valid to have void main(). Other functions that you (or others write) can, however, be void. For example, signal handlers don't return a value, so they're void. Original Posting: >From Carlos Sevillano <carlos_sevillano@ureach.com> Add contact To sunmanagers@sunmanagers.org Subject void main(int argc, char *argv[]) question Solaris 2.8 GCC 2.95.3 "SMCgcc" jedit, ddd, gdb In trying to compile some old test Unix C programs... I was not able to compile the programs. After a while of tying to debug the programs that were of format: void main(int argc, char *argv[]) or void main(int argc, char *argv[]) failed to compile with errors on main: # gcc -pg prog03.02.c -o prog032 prog03.02.c: In function `main': prog03.02.c:15: warning: return type of `main' is not `int' The program compiled fine by removing "void" from the function main. Why do those old UNIX programs used void ... it must have been valid at some point ... there are quite a lot of such programs in Unix books ei Practical Unix programing, Rago Unix System Programming, Oreilly Unix System Programming etc. What was the use of "void" as originally intended (granted it seems not work now days). Did the language evolved (I tried -ansi on compiling trying with the same type of errors). There is a lot of usage of void in the Unix programming literature. My question is in a nutshell can someone explain the usage where it is ok to use void () and where it is not keeping in mind there a quite a lot of function prototypes that use void ei: main(void) void handler(int) void main(int argc, char *argv[]) void main(int argc, char *argv[]) There is also: main() int main(int argc, char *argv[]); Carlos ________________________________________________ Get your own "800" number Voicemail, fax, email, and a lot more http://www.ureach.com/reg/tag _______________________________________________ sunmanagers mailing list sunmanagers@sunmanagers.org http://www.sunmanagers.org/mailman/listinfo/sunmanagersReceived on Tue Sep 14 10:07:54 2004
This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:43:37 EST