Newsgroups: comp.lang.c
Path: utzoo!sq!msb
From: msb@sq.sq.com (Mark Brader)
Subject: Re: New 'n' Improved comp.lang.c FAQ List
Message-ID: <1991Apr6.040632.23674@sq.sq.com>
Organization: SoftQuad Inc., Toronto, Canada
References: <1780@mti.mti.com> <11742@dog.ee.lbl.gov> <1991Apr04.023548.28947@ima.isc.com>
Date: Sat, 6 Apr 91 04:06:32 GMT
Lines: 30

Karl Heuer (karl@ima.isc.com) writes:
> In article <11742@dog.ee.lbl.gov> torek@elf.ee.lbl.gov (Chris Torek) writes:
> >You must use two levels of macro evaluation:
> >	#define XQUOTE(a)	QUOTE(a)	/* expanded quote */
> >		static char retbuf[sizeof(XQUOTE(INT_MIN))];
> 
> But you still don't necessarily get the right answer, since INT_MIN might not
> be implemented as a simple digit-string.  If <limits.h> defines INT_MIN as
> (1<<31), the sizeof() will only return 8.

Gee, this is fun!  Okay, if we're assuming ANSI C, we have CHAR_BIT
available, and this is also the conversion factor from sizeof's result
to bits.  Therefore...

	#define CHAR_BIT 8
	#define	LOG2	.30102999566398119521373889472449302676818988146211
			/* base 10 logarithm of 2 */

	static char retbuf[(int)((sizeof(int)*CHAR_BIT - 1) * LOG2) + 3];
			/* -1 corrects for the sign bit; +3 corrects for
			   a possible '-', for the trailing '\0', and for
			   the downward rounding on conversion to int. */

Simple, eh?
-- 
Mark Brader, Toronto	    "If you feel [that Doug Gwyn] has a bad attitude,
utzoo!sq!msb, msb@sq.com     then use lint (or Chris Torek...)" -- Joe English

This article is in the public domain.  *I* certainly don't want anything
further to do with it.
