delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1992/09/30/17:53:40

From: Eric Backus <ericb AT lsid DOT hp DOT com>
Subject: Re: abs()olutely hopeless?
To: djgpp AT sun DOT soe DOT clarkson DOT edu (djgpp)
Date: Wed, 30 Sep 92 14:29:30 PDT
Mailer: Elm [revision: 66.25]

> >#define ABS(x) ((x) < 0 ? -(x) : (x))
> >so that e.g. ABS(-a+b) works correctly. 
> 
> Should that not actually be:
> 
> 	#define ABS(x) \
> 		({typedef _tx = (x); \
> 			_tx _x = (x); \
> 			_x < 0 ? -_x : _x; })
> 
> This yields the correct result when you do:
> 	foo = abs(bar += 17);

Yes, this is better, but it is a GNU-cc-only extension to the C
language.  The non-typedef solution is pure ANSI C.
--
				Eric Backus
				ericb%hplsla AT hplabs DOT hp DOT com
				(206) 335-2495

- Raw text -


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