Change in Hex Literal Interpretation with C 6.0 and 7.0

Last reviewed: July 17, 1997
Article ID: Q58488
6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50
MS-DOS                 | OS/2       | WINDOWS
kbtool kbfasttip

The information in this article applies to:

  • Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
  • Microsoft C for OS/2, versions 6.0, and 6.0a
  • Microsoft C/C++ for MS-DOS, versions 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 1.5

SUMMARY

ANSI mandates that the \0x... character constant doesn't end until the first nonhexadecimal character, regardless of how many characters that may involve.

MORE INFORMATION

Microsoft C version 5.1 ends the constant at the third character or just before the first nonhexadecimal character, regardless of whether characters after the third were valid hexadecimal characters.

Therefore, while "abc\x34564gh" is represented in version 5.1 as shown below

   Character       a   b   c   E   6   4   g   h
   Hex            61  62  63  45  36  34  67  68
   Decimal        97  98  99  69  54  52 103 104

note that the character after "c" is "\x345". Because this has to fit in 1 byte, and the rules of C say that the low bits are preserved when converting to a shorter type, "\x345" is equivalent to "\x45".

In Microsoft C version 6.0, the string is represented as follows:

   Character       a   b   c   d   g   h
   Hex            61  62  63  64  67  68
   Decimal        97  98  99 100 103 104

Note that "\x34564" -- the character after the "c" -- is equivalent to "\x64" for the same reason "\x345" was equivalent to "\x45" under the old rules above. The hexadecimal constant includes all the characters up to but not including the nonhexadecimal character "g" because of the rule change described above. C 5.1 compiles such code without warnings. C 6.0 issues the following warning if the character is too large to fit in 1 byte:

   C4139: '0x4564' : hex escape sequence is out of range

C/C++ version 7.0 and Visual C/C++ version 1.0 and above will issue an error similar to the following message:

   C2022: '214372': too big for character

The '214372' value is the decimal representation of the hexadecimal value 0x34564.

The best workaround for this change is to end the string immediately after the hex character, and restart it. For example, the string "abc\x34""564gh" is interpreted by all versions of the compiler as containing the following:

   Character       a   b   c   4   5   6   4   g   h
   Hex            61  62  63  34  35  36  34  67  68
   Decimal        97  98  99  52  53  54  52 103 104

Such a string works properly when compiled using any ANSI-compatible compiler.


Additional reference words: kbinf 1.00 1.50 6.00 6.00a 6.00ax 7.00 8.00
8.00c
KBCategory: kbtool kbfasttip
KBSubcategory: CLngIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.