Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print adresses with printf %016x vs %p on 64bit systems [duplicate]

Tags:

c

pointers

printf

I have some trouble with a book that I am currently reading about C and assembly. The author uses a 32 bit environment while I am using 64-bit. The problem is that the author often uses

printf("%08x", &var);

To print addresses which works fine on 32 bit. But when I run this on 64-bit I get only half of the address while %p gives me the whole address... So why is this so? I of course use %016xinstead of %08x.

The author only uses %p for pointers. So when should I use what?

like image 923
crush3dice Avatar asked Oct 15 '25 08:10

crush3dice


1 Answers

To print pointers in printf, using %p with void * type is the way according to the C standard.

printf("%p", (void *)&var);
like image 89
Yu Hao Avatar answered Oct 16 '25 22:10

Yu Hao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!