Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!umich!sharkey!bnlux0!max!drs From: drs@max.bnl.gov (Dave Stampf) Newsgroups: comp.sys.mac.programmer Subject: Re: THINKC 4.0.2 #define is short Keywords: #define preprocessor short int << Message-ID: <2312@bnlux0.bnl.gov> Date: 14 Nov 90 18:02:13 GMT References: <2374@moscom.UUCP> Sender: news@bnlux0.bnl.gov Reply-To: drs@max.UUCP (Dave Stampf) Organization: Brookhaven National Lab, Upton NY Lines: 39 In article <2374@moscom.UUCP> tcm@moscom.UUCP (Tom Maszerowski) writes: > >Here's a gotcha in THINK C 4.0.2 that got me. It's probably in the >manual somewhere, but a cursory galnce didn't reveal it ( I couldn't >find any reference to #define or the preprocessor anywhere in the index). >Anyway, this is what happened: > >given: > >#define MaxAllocation (1<<16) > >sets MaxAllocation to 0. > >I believe that this is because the preprocessor assumes that >MaxAllocation is an int ( 16 bits ) but the result of the shift is >0x10000, which requires a long ( 32 bits ). I realize that compiler >implementers are free to make an int any size they want, but this is the >first 68000 C compiler I've used where sizeof(int) != sizeof(long). By The preprocessor assumes nothing. The word MaxAllocation is replaced as *text* everywhere with (1<<16). When you shift a 16 bit integer 16 places, you get 0. To get what you want, try #define MaxAllocation (1L<<16) There are many compilers for 68000's and PC's which have sizeof(int) != sizeof(long). In fact, the reason for giving programmers the sizeof operator is precisely to allow for varying architectures - get used to it. By the way, I suspect that you will frequently run into the problem of Think C routines returning an int when you *know* that the routine returns a something*. I have a note above my mac that says "int != char*". You may want to do the same - it has saved me *hours* of debugging. < dave (I agree however that it would be helpful if Symantec would describe the operation of the preprocessor and perhaps even have a mode where you saw that preprocessed text fed to the compiler (-E) option.)