Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site chemabs Path: utzoo!watmath!clyde!cbosgd!apr!chemabs!garyd From: garyd@chemabs (garyd) Newsgroups: net.lang.c Subject: C Preprocessor BUG with Hexidecimal Constants Message-ID: <152@chemabs> Date: Fri, 22-Mar-85 15:34:36 EST Article-I.D.: chemabs.152 Posted: Fri Mar 22 15:34:36 1985 Date-Received: Sat, 23-Mar-85 04:18:48 EST Organization: Chem Abs Serv, Columbus OH Lines: 45 C Preprocessor BUG in IS/3 and System V with Hexidecimal Constants. When using hexadecimal constants as part of a #IF 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