Using a define that hasn't been #defined

David T. Sandberg dts at quad.sialis.mn.org
Wed Oct 3 18:47:07 AEST 1990


In article <1990Oct3.022132.9245 at ux1.cso.uiuc.edu> gordon at osiris.cso.uiuc.edu (John Gordon) writes:
>	In fact, there is a very good way to avoid this kind of error: use the
>compiler directives #ifdef and #ifndef.  These check to see if a given 
>identifier has or has not been #define'd.

I'm not the one who asked about this in the first place, but I really
don't see how that would be expected to alleviate the situation.  You
see, I recently encountered a bug of a similar character (heh), as
follows:

#define SYSV

{
    ...
#ifdef SYSY
    ...
#endif
    ...
}

In this situation, whether you use #if or #ifdef doesn't make any
difference.  To exasperate the situation, I was using a terminal
upon which capital 'V's and 'Y's look even more similar than normal,
and the problem resulting from the #ifdef-ed code's absence only
manifested itself much later, in another module entirely separate
from the one where the bug was hiding.

That one took days, and large chunks of my hair, to find.  B-(

I suppose that, if one is very concerned about this and is using
an ANSI-compliant preprocessor, one could do something like this:

#include <stdio.h>

#define SYSV	1
#define DOS	2
#define	OSTYPE	SYSV

main()
{
#if OSTYPE == SYSV
	printf( "This is the System V-specific code.\n" );
#elif OSTYPE == DOS
	printf( "This is the System V-specific code.\n" );
#else
#error OSTYPE undefined or mistyped
#endif
}

-- 
 \\         David Sandberg          \     ,=,       ,=,           \\
 //     dts at quad.sialis.mn.org      /     | |uadric '=,ystems     //
 \\  uunet!rosevax!sialis!quad!dts  \     '=\       `='           \\



More information about the Comp.lang.c mailing list