What is the behavior of printing NULL with printf's %s specifier? What is the behavior of printing NULL with printf's %s specifier? c c

What is the behavior of printing NULL with printf's %s specifier?


First things first: printf is expecting a valid (i.e. non-NULL)pointer for its %s argument so passing it a NULL is officiallyundefined. It may print "(null)" or it may delete all files on yourhard drive--either is correct behavior as far as ANSI is concerned(at least, that's what Harbison and Steele tells me.)

That being said, yeah, this is really wierd behavior. It turns outthat what's happening is that when you do a simple printf like this:

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

gcc is (ahem) smart enough to deconstruct this into a call toputs. The first printf, this:

printf("test %s\n", NULL);

is complicated enough that gcc will instead emit a call to realprintf.

(Notice that gcc emits warnings about your invalid printf argumentwhen you compile. That's because it long ago developed the ability toparse *printf format strings.)

You can see this yourself by compiling with the -save-temps optionand then looking through the resulting .s file.

When I compiled the first example, I got:

movl    $.LC0, %eaxmovl    $0, %esimovq    %rax, %rdimovl    $0, %eaxcall    printf      ; <-- Actually calls printf!

(Comments were added by me.)

But the second one produced this code:

movl    $0, %edi    ; Stores NULL in the puts argument listcall    puts        ; Calls puts

The wierd thing is that it doesn't print the following newline.It's as though it's figured out that this is going to cause a segfaultso it doesn't bother. (Which it has--it warned me when I compiledit.)


As far as the C language is concerned, the reason is that you're invoking undefined behavior and anything can happen.

As for the mechanics of why this is happening, modern gcc optimizes printf("%s\n", x) to puts(x), and puts does not have the silly code to print (null) when it sees a null pointer, whereas common implementations of printf have this special case. Since gcc can't optimize (in general) non-trivial format strings like this, printf actually gets called when the format string has other text present in it.


Section 7.1.4 (of C99 or C11) says:

§7.1.4 Use of library functions

¶1 Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined.

Since the specification of printf() says nothing about what happens when you pass a null pointer to it for the %s specifier, the behaviour is explicitly undefined. (Note that passing a null pointer to be printed by the %p specifier is not undefined behaviour.)

Here is the 'chapter and verse' for the fprintf() family behaviour (C2011 — it is a different section number in C1999):

§7.21.6.1 The fprintf function

s     If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type. [...]

     If an l length modifier is present, the argument shall be a pointer to the initial element of an array of wchar_t type.

p     The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

The specifications for the s conversion specifier preclude the possibility that a null pointer is valid since the null pointer does not point to initial element of an array of the appropriate type. The specification for the p conversion specifier does not require the void pointer to point at anything in particular and NULL is therefore valid.

The fact that many implementations print a string such as (null) when passed a null pointer is a kindness that is dangerous to rely upon. The beauty of undefined behaviour is that such a response is permitted, but it is not required. Similarly, a crash is permitted, but not required (more's the pity – people get bitten if they work on a forgiving system and then port to other less forgiving systems).