I want to get a detailed explanation on the difference between using %d
and %p
type for printing pointer
.
Also
Why does %p
return hexadecimal?
What are the cases when %d
and %p
return different values?
Does datatype only represent the way the user wants the output or it has something to do with the memory locations too?
For the program to be well-defined, the format specifier must match the type of the argument. Therefore you can use %p
but not %d
to print out pointers. (The latter might happen to work on some architectures but is technically undefined behaviour.)
The primary reason you can't freely interchange %d
and %p
is that ints
and pointers don't have to have the same size.
The format in which pointers are printed out is architecture-specific (pointers can have different size or indeed different structure). It is, however, common to transcribe memory addresses in hexadecimal, so this is what %p
usually does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With