Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the behavior of the conversion specifier `p` with `NULL` pointer?

Tags:

c

null

printf

I wonder if does:

void *ptr = NULL;
printf("%p\n", ptr);

Will always gives (nil) output?

Does it depend on standard library implementation, or it's a C99 standard specification?

like image 321
patseb Avatar asked May 05 '12 11:05

patseb


2 Answers

On my system it yields (null) so I guess it's implementation defined. More generally, everything %p prints is implementation-defined:

7.21.6.1

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.

like image 130
cnicutar Avatar answered Oct 15 '22 10:10

cnicutar


Will always gives (nil)?

Not at all. On my machine (Mac with i686-apple-darwin11-llvm-gcc-4.2) it prints 0x0.

like image 33
MByD Avatar answered Oct 15 '22 09:10

MByD