INF: How the NULL Character Is Handled by printf() Functions

ID Number: Q38296

4.00 5.00 5.10 6.00 6.00a 6.00ax | 4.00 5.00 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

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.

More Information:

The output of the following program is the result of expected

behavior:

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

char buffer[30] ;

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: 5.00 5.10 6.00 6.00a 6.00ax