Microsoft KB Archive/64640

{|
 * width="100%"|

DOC: Online Help Solution for C4139 Warning Is Incorrect

 * }

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 &quot;\x&quot; 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(&quot;\x7bell\n&quot;); /* Error-causing code */ Next, the following workaround is given to resolve the problem: printf(&quot;\x007bell\n&quot;); /* 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 &quot;\x&quot; as part of the constant. This means that &quot;\x007bell&quot; is interpreted as a 5-digit hexadecimal value followed by &quot;ll&quot;. Because &quot;\x007be&quot; 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(&quot;\x007&quot;&quot;bell\n&quot;); or printf(&quot;\0x007bell\n&quot;); - char TypeArray[] = &quot;\x007&quot;&quot;bell&quot;; printf(&quot;%s\n&quot;, TypeArray); Note: According to the ANSI standard, adjacent string literals are concatenated after escape sequences have been calculated. - printf(&quot;\007bell\n&quot;); /* 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: &quot;Differences between C 5.1 and 6.0,&quot; under the subtitle &quot;Hexadecimal Constants in Strings.&quot; Additional reference words: 1.00 6.00 6.00a 6.00ax 7.00 8.00 KBCategory: kbprg kbdocerr KBSubcategory: CLIss

Keywords : kb16bitonly

Issue type :

Technology : kbVCsearch kbAudDeveloper kbCVCComp