compile-time function - (nf)

jack at hp-dcde.UUCP jack at hp-dcde.UUCP
Fri Oct 14 17:29:54 AEST 1983


#N:hp-dcde:20000001:000:517
hp-dcde!jack    Oct 12 19:00:00 1983

I want to define a compile-time function.  For example:

#define LOG2(n)	\
#if n==1	\
	0	\
#endif		\
#if n==2	\
	1	\
#endif		\
#if n==4	\
	2	\
#endif		\
... and so on ...

	i = LOG2(4);

Which I want to produce i = 2.

However, it seems that the whole macro definition gets put together
on a single line before the preprocessor checks for #if's, so the
#if's aren't recognized.  How can I (for the general case, not just LOG2)
do something like this?

					-Jack Applin
					Hewlett-Packard
					(hplabs!hp-dcd!jack)



More information about the Comp.lang.c mailing list