[net.lang.c] CC bug test

nazgul@apollo.uucp (Kee Hinckley) (01/30/85)

...

This applies mainly to BSD users, but everyone is free to try.

I've seen several pieces of code out there that say "#ifdef V4.2".
Nothing wrong with that, right?  WRONG.  The K&R state that macros
are identifiers, and identifiers DON'T have '.'s in them.  If they
did how would you do structure references?  I have been told that
in fact "V4.2" works only because the Berkeley compilers ignore the
".2".  If this is the case then two things are true.  The following
shell script is NOT going to do what you expected, and someone had
better come up with a better "#ifdef".  I rather hope that the latter
is done regardless of the former, since strict C compilers won't compile
a program that says "#ifdef V4.2".  Which makes it a bit of a pain for
those of us who port the torrents of stuff coming across the net.

Here goes.

main()
{

    printf("Currently compiling only");
#ifdef V4
    printf(" V4");
#endif
#ifdef V4.1
    printf(" V4.1");
#endif
#ifdef V4.2
    printf(" V4.2");
#endif
    printf(" lines.\n");

}

                                                    -Kee Hinckley
                                          ...decvax!wanginst!apollo!nazgul

P.S.  I don't think there are any syntax errors in the above program (other
      than the one in question), but I don't guarantee it since I can't
      compile it!

west@sdcsla.UUCP (Larry West) (01/31/85)

In article <2479686f.8e4@apollo.uucp> nazgul@apollo.uucp (Kee Hinckley) writes:
>...
>
>main()
>{
>    printf("Currently compiling only");
>#ifdef V4
>    printf(" V4");
>#endif
>#ifdef V4.1
>    printf(" V4.1");
>#endif
>#ifdef V4.2
>    printf(" V4.2");
>#endif
>    printf(" lines.\n");
>}

Hmmm.   On both our Vax and Sun (both 4.2bsd, more or less), the result
of running this program is: "Currently compiling only lines.".   I hope
these "#ifdef"s are not in widespread use.    I haven't noticed them
in any code I've seen (user or system).
-- 

--|  Larry West, UC San Diego, Institute for Cognitive Science
--|  UUCP:	{decvax!ucbvax,ihnp4}!sdcsvax!sdcsla!west
--|  ARPA:	west@NPRDC	{ NOT: <sdcsla!west@NPRDC> }

ksbszabo@wateng.UUCP (Kevin Szabo) (02/04/85)

In article <780@sdcsla.UUCP> west@sdcsla.UUCP (Larry West) writes:
>In article <2479686f.8e4@apollo.uucp> nazgul@apollo.uucp (Kee Hinckley) writes:
>>main()
>>{
>>    printf("Currently compiling only");
>>#ifdef V4
>>    printf(" V4");
>>#endif
>>#ifdef V4.1
>>    printf(" V4.1");
>>#endif
>>#ifdef V4.2
>>    printf(" V4.2");
>>#endif
>>    printf(" lines.\n");
>>}
>
>Hmmm.   On both our Vax and Sun (both 4.2bsd, more or less), the result
>of running this program is: "Currently compiling only lines.".

Well, on our BSD4.2 machine, the following command sequence
	cc -DV.2 test.c; a.out
produces:
	Currently compiling only V4 V4.1 V4.2 lines.

as the original author stated. To repeat the author (in case anybody
missed the original posting). A `.' (period) is not legal part of an
identifier, either in the preprocessor or in C. BSD software allows
you to use a period in preprocessor identifiers, but it quietly truncates
the period and trailing part of the string. Could cause some confusing
bugs...

					Kevin

P.S. Plug time: In Henry Spencer's annotated Indian Hill C Style and Coding
standards he states: If you #ifdef dependencies, make sure that if no machine
is specified, the result is a syntax error, not a default machine.
..Why am I saying this? I don't know, just seemed like a good time to spout
off. I know that following this advice has saved some users of my code a
good deal of heartache..
-- 
Kevin Szabo  watmath!wateng!ksbszabo (U of Waterloo VLSI Group, Waterloo Ont.)