Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!bbn.com!nic!chaos.cs.brandeis.edu!chaos!phils From: phils@chaos.cs.brandeis.edu (Phil Shapiro) Newsgroups: comp.sys.mac.programmer Subject: Re: THINKC 4.0.2 #define is short Message-ID: Date: 14 Nov 90 14:28:01 GMT References: <2374@moscom.UUCP> Sender: @chaos.cs.brandeis.edu Organization: Symantec Corp. Lines: 38 In-Reply-To: tcm@moscom.UUCP's message of 13 Nov 90 18:11:48 GMT 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 ). [ ... ] Close. Since ThC's int size (or 'word' size) is 16 bits, all integer constants that don't need to be put in long integers aren't. If you specify a constant that isn't outside of the 16 bit integer range, it will be placed into the 16 bit storage, and consequently the expression is evauated using 16 bit storage. To force an extension to a long integer, you can use: #define MacAllocation ((long)1 << 16) or (preferred): #define MaxAllocation (1L<<16) To write really really ANSI standard code, you probably would want to always use unsigned integers, as bitshifts of signed integers are left unspecified by the ANSI standard. -phil -- Phil Shapiro Technical Support Analyst Language Products Group Symantec Corporation Internet: phils@chaos.cs.brandeis.edu