Microsoft KB Archive/38296

= 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 &quot;0&quot;) 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

char buffer[30] ;
 * 1) include 

void main {

printf(&quot;Before,%c,After\n&quot;, '\0') ; sprintf(buffer, &quot;Before,%c,After\n&quot;, '\0') ; printf(buffer) ; }

/* end of sample program */ Output :

Before, ,After Before,