Microsoft KB Archive/38296

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Microsoft Knowledge Base

How the NULL Character Is Handled by printf() Functions

Last reviewed: July 22, 1997
Article ID: Q38296

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a | 1.00 1.50 | 1.00 2.00 4.00

MS-DOS                      | OS/2            | WINDOWS   | WINDOWS NT

kbprg kbfasttip

The information in this article applies to:

The C Run-time (CRT), included with:

    - Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0ax
    - Microsoft C for OS/2, versions 5.1, 6.0, and 6.0a
    - Microsoft C/C++ for MS-DOS, version 7.0
    - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, and 4.0

When the C run-time function printf(), fprintf(), or sprintf() encounters the character-conversion specifier %c in its format-control string, it will convert the corresponding argument of int type to unsigned char type and write the resulting value to output. Therefore, if the argument is a NULL character, the value 0 (not the character "0") is written to the output.

The output can be stdout, a file [with fprintf()] or a string [with sprintf()]. In the case of stdout, the NULL character is ignored by the display device. In the case of string, the NULL character will be interpreted as a terminator character when the resulting string is used later in the program.

The output of the following program is the result of expected behavior:

Sample Code

/* Compile options needed: none
*/

#include <stdio.h>
char buffer[30] ;

void main()

{

   printf("Before,%c,After\n", '\0') ;
   sprintf(buffer, "Before,%c,After\n", '\0') ;
   printf(buffer) ;

}

/* end of sample program */

Output :

Before, ,After Before,


Additional reference words: kbinf 1.00 1.50 2.00 4.00 5.10 6.00 6.00a

6.00ax
7.00
KBCategory: kbprg kbfasttip
KBSubcategory: CRTIss
Keywords : CRTIss kbfasttip kbprg
Version : 5.10 6.00 6.00a 6.00ax 7.00 | 5.
Platform : MS-DOS NT OS/2 WINDOWS


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 22, 1997
©1997 Microsoft Corporation. All rights reserved. Legal Notices.