New 'n' Improved comp.lang.c FAQ List

Mark Brader msb at sq.sq.com
Sat Apr 6 14:06:32 AEST 1991


Karl Heuer (karl at ima.isc.com) writes:
> In article <11742 at dog.ee.lbl.gov> torek at 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 at 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.



More information about the Comp.lang.c mailing list