Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!ames!pasteur!ucbvax!dewey.soe.berkeley.edu!oster From: oster@dewey.soe.berkeley.edu (David Phillip Oster) Newsgroups: comp.sys.mac.programmer Subject: LSC 3.0 Boolean Bug/Feature Message-ID: <25467@ucbvax.BERKELEY.EDU> Date: 5 Aug 88 16:25:13 GMT References: <3653@bnrmtv.UUCP> <586@mailrus.cc.umich.edu> Sender: usenet@ucbvax.BERKELEY.EDU Reply-To: oster@dewey.soe.berkeley.edu.UUCP (David Phillip Oster) Distribution: na Organization: School of Education, UC-Berkeley Lines: 47 LightSpeed C 3.0 compiles a slightly different language than the older LightSpeed Cs, and it can get you into trouble. You will have no trouble if you religiously use protypes. Use prototypes. Under the lightspeed C, the following program will always print "False": ----- file 1 ------ #include main(){ if(Oster()){ printf("True"); }else{ printf("False"); } } ----- file 2 ----- #include Boolean Oster(){ return FALSE; } ------------------- Under LightSpeed C version 3.0, this program will print "True", but only some of the time! You see, in file 1, the function Oster() wasn't explicitly declared, so its declaration defaults to int Oster(); But Oster() is really of type Boolean, which is a small enum, in paticular, an enum small enough to fit in a char. Under the old C, this still worked, because char function returns were sign extended to int. Under the new C, Oster() returns a char, but main expects to see an int, and nobody is clearing the high byte of the return value. That high byte is whatever happened to be in the function return register. Sometimes its zero, sometimes its not. If it happens to be zero, main will print "False". Otherwise, main will print "True". In article <586@mailrus.cc.umich.edu> shane@um.cc.umich.edu (Shane Looker) writes: >I would strongly advise using prototypes. They are very handy, since they >can not only save you on problems like this, but they allow the compiler to >know what you want to pass, not second guess you. > >Shane Looker >Looker@um.cc.umich.edu I couldn't say it more eloquently. --- David Phillip Oster --When you asked me to live in sin with you Arpa: oster@dewey.soe.berkeley.edu --I didn't know you meant sloth. Uucp: {uwvax,decvax,ihnp4}!ucbvax!oster%dewey.soe.berkeley.edu