delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/12/27/17:42:20

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
Date: Thu, 27 Dec 2001 17:39:27 -0500
Message-Id: <200112272239.fBRMdR232033@envy.delorie.com>
From: DJ Delorie <dj AT delorie DOT com>
To: djgpp AT delorie DOT com
In-reply-to: <daed704d.0112271423.309fcdeb@posting.google.com>
(matt AT mattshouseofpain DOT com)
Subject: Re: DJGPP: #define problems
References: <daed704d DOT 0112271423 DOT 309fcdeb AT posting DOT google DOT com>
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> #define BASEVAL1    233
> #define BASEVAL2    BASEVAL1 + 29
> #define BASEVAL3    233
> #define BASEVAL4    BASEVAL1 + 57
> 
>    printf("baseval4 - baseval2 [28] = %d\n\n", BASEVAL4 - BASEVAL2);

Use "gcc -E" and you'll see what's happening:

BASEVAL4 - BASEVAL2
BASEVAL1 + 57 - BASEVAL1 + 29
233 + 57 - 233 + 29

Yup, 57 + 29 is 86, not 28.

I suggest paranoid parens in your defines:

#define BASEVAL1    233
#define BASEVAL2    (BASEVAL1 + 29)
#define BASEVAL3    233
#define BASEVAL4    (BASEVAL1 + 57)

You should always use parens (1) around use of arguments in a #define
taking arguments, and (2) around the whole #define's value, when it's
an expression.  Example:

#define TWICE(A)  ((A)+2)

The inner parens are in case you do TWICE(y^4), the outer parens are
in case you do TWICE(j)*4

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019