Re: My idea of fully-portable C code
  Home FAQ Contact Sign in
comp.arch.embedded only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: My idea of fully-portable C code         

Group: comp.arch.embedded · Group Profile
Author: Thad Smith
Date: May 15, 2008 20:45

Chris H wrote:
> In message
> 26g2000hsk.googlegroups.com>,
> Tomás Ó hÉilidhe lavabit.com> writes
>> On May 13, 7:59 pm, Hans-Bernhard Bröker
>> wrote:
>>>> #define TOTAL_BITS_NEEDED (QUANTITY_CHUNKS * BITS_PER_CHUNK)
>>>
>>> Bad macro. Parentheses missing around QUANTITY_CHUNKS and
>>> BITS_PER_CHUNK leave the code vulnerable to funny definitions of those
>>> constants (e.g. #define BITS_PER_CHUNK 2+3) The same problem occurs
>>> repeatedly later on.
>>
>> You're confusing things. A macro is a self-contained, self-sufficient
>> entity. If I have a macro called QUANTITY_CHUNKS, then it should be
>> able to be used on its own without parentheses. If it needs
>> parentheses, then it is ill-formed.
>
> Now you are running counter to most standards and guides for portability
> and safety.

I suspect Chris misunderstands Tomas's point: that macros defining numeric
expressions should have surrounding parentheses in the macro definition so
they aren't required where they are used. So

#define FOO (BAR+3)
x = FOO+4;

rather than

#define FOO BAR+3
x = (FOO)*4;

Hans-Bernard's point, of being paranoid of externally-defined macros and
surrounding those with parentheses is good, of course.

--
Thad
no comments
diggit! del.icio.us! reddit!