DOCERR: Online Help Solution for C4139 Warning Is Incorrect

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

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE), included with:

        - 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, version 1.0
    

SYMPTOMS

Using hexadecimal constants in strings results in the following compiler warning

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

if the hexadecimal digits following the "\x" escape character evaluate to a number too large to be converted to an ASCII character.

The online help systems for Microsoft C versions 6.0, 6.0a, and 6.0ax, and QuickC versions 2.5 and 2.51, show an example of code that produces the following warning:

   printf("\x7bell\n");   /* Error-causing code */

Next, the following workaround is given to resolve the problem:

   printf("\x007bell\n");  /* Supposed to resolve problem */

Unfortunately, the second printf statement produces the same error as the first.

With C versions 6.0 and later, the first printf generates the error:

   C2022: '1982' : too big for character

The online help for C version 8.0 for MS-DOS provides a correct workaround. However, it incorrectly references the octal digits in the workaround as hexadecimal digits. The online help for Visual C++ 1.5 and Visual C++ 1.0 provide the correct information.

CAUSE

The compiler treats every potential hexadecimal digit following the "\x" as part of the constant. This means that "\x007bell" is interpreted as a 5-digit hexadecimal value followed by "ll". Because "\x007be" equals 1982 decimal, a C2022 error will be generated because this value is too large for an ASCII character.

RESOLUTION

Three valid workarounds are listed below:

  • printf("\x007""bell\n");

         or
    
    printf("\0x007bell\n");
  • char TypeArray[] = "\x007""bell";

    printf("%s\n", TypeArray);

    Note: According to the ANSI standard, adjacent string literals are concatenated after escape sequences have been calculated.

  • printf("\007bell\n"); /* Use Octal */

    Note: This workaround uses an octal constant rather than a hexadecimal constant, and is a good solution if portability is a concern. Also note that the documentation for C/C++ version 7.0 and Visual C++ give this as the correct workaround, but call it a 3-digit hexadecimal value rather than a 3-digit octal value.

REFERENCES

Additional information is given in the C 6.0 README.DOC file in Part 2: "Differences between C 5.1 and 6.0," under the subtitle "Hexadecimal Constants in Strings."


Additional reference words: 1.00 6.00 6.00a 6.00ax 7.00 8.00
KBCategory: kbprg kbdocerr
KBSubcategory: CLIss
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.