C Preprocessor BUG with Hexidecimal Constants

garyd garyd at chemabs
Sat Mar 23 06:34:36 AEST 1985


C Preprocessor BUG in IS/3 and System V with Hexidecimal Constants.

When using hexadecimal constants as part of a #IF <constant-expression> the 
preprocessor evaluates the hexadecimal digits A through F (or a - f) not as 10 
to 15 (decimal), but rather as 0 to 5.  This holds true for each hexidecimal 
nybble.  Thus 0xff is really interpreted by the preprocessor as a binary 
01010101.  And 0xa is evaluated as zero, which can easily be demonstrated by 
the following C program which by all rights should print true: 

	main()
	{
	#if (0XA)
		printf("true\n");
	#else
		printf("false\n");
	#endif
	}

This bug was first discovered in the IS/3 version of the C Preprocessor, and
was found to be hiding in the System V source code also.  The Whitesmiths' C
preprocessors handle this properly.  This error is in the yylex.c source
code file which is part of the cpp program.  The function containing the
error is tobinary(), which follows:

73      case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
74              t = c-'a'; if (b>10) break;
75      case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
76              t = c-'A'; if (b>10) break;

This code might be corrected as follows:

73      case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
74              t = c-'a'+10; if (b>10) break;
75      case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
76              t = c-'A'+10; if (b>10) break;


Gary A. Davis
Senior Engineer
Chemical Abstracts Service
UUCP:  ..{ihnp4|mhuxl|ho95b|ulysses}!cbosgd!osu-eddie!apr!chemabs!garyd
AT&T:  (614) 421-3600 X2355
USPS:  Chemical Abstracts Service
       P.O. Box 3012
       Columbus, Ohio 43210



More information about the Comp.lang.c mailing list