What Is The Difference Between %p And 0x%08x - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs What is the difference between %p and 0x%08x Ask Question Asked 9 years, 1 month ago Modified 9 years, 1 month ago Viewed 14k times 3

the question says it all. In c, when trying to get the address of a pointer or something, I know the commands:

%p

and

0x%08x

I know that they both return addresses, but why use one over another?

Check this out:

int main () { int i[1] = {1}; int *int_pointer; int_pointer = i; printf("0x%08x\n",&i); printf("0x%08x\n", int_pointer); printf("%p\n",&i); printf("%p\n", int_pointer); }

This code yields this response: 0x60506b00 0x60506b00 0x7ffe60506b00 0x7ffe60506b00

if both refer to the addresses, then how come they are different depending on if you use %p or 0x%08x?

Which is the actual address?

Share Improve this question Follow edited Oct 25, 2015 at 0:39 yasgur99 asked Oct 25, 2015 at 0:02 yasgur99's user avatar yasgur99yasgur99 8382 gold badges12 silver badges37 bronze badges 1
  • 2 Your pointers are 64 bit, and %08x only prints 32 bits of hex. – JS1 Commented Oct 25, 2015 at 2:43
Add a comment |

2 Answers 2

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 12

printf("%p", p) expects a void* pointer as argument. printf("0x%08x", n) expects an unsigned int as argument.

Passing a pointer to the latter might work anyway provided that an unsigned int and a pointer are "similar enough" in their representations on the stack. But to be safe and correct no matter what your machine is, always use "%p" with pointers.

Share Improve this answer Follow answered Oct 25, 2015 at 0:08 aschepler's user avatar aschepleraschepler 72.1k10 gold badges112 silver badges170 bronze badges 5
  • so if i was using a char, I would just convert it into an int. Correct me if im wrong – yasgur99 Commented Oct 25, 2015 at 0:15
  • i edited my original post to reflect an example of the usage and i'm still confused. do you mind clarifying it? – yasgur99 Commented Oct 25, 2015 at 1:29
  • @yasgur99: it appears that pointers are 64-bits wide on your machine, but the %08x conversion specifier only prints the low-order 32 bits. Pointers are not unsigned integers, and they don't have to have the same size or representation as unsigned integers. Always use %p to print pointer values, at least if you want meaningful output. – John Bode Commented Oct 25, 2015 at 4:26
  • so what should I use if I wanted to print the address of integers or other variables – yasgur99 Commented Oct 25, 2015 at 4:28
  • @yasgur99: assuming you want to print the address of a variable named a (regardless of type), you would write printf( "%p\n", (void *) &a ); – John Bode Commented Oct 26, 2015 at 16:59
Add a comment | 4

Because %p is the portable way of doing it. Even though a pointer stores an address, it is not an integer (and the standard does not require a compiler to use integers for pointer representations), hence it is not appropriate to use an integer specifier.

Share Improve this answer Follow answered Oct 25, 2015 at 0:08 vsoftco's user avatar vsoftcovsoftco 56.4k12 gold badges147 silver badges263 bronze badges 2
  • what about when using a char – yasgur99 Commented Oct 25, 2015 at 0:10
  • a char is implicitly convertible to an integer. – vsoftco Commented Oct 25, 2015 at 0:10
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • We'll Be In Touch - A New Podcast From Stack Overflow!
  • The app that fights for your data privacy rights
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...
7 What is the difference between {0} and ""? 96 What is %0|%0 and how does it work? 1 Machine level difference in format specifiers %s %d and %c for null byte 1 What is the difference between using 0 and 0x0 in SQL Server? 7 Why is the format of %p and %x different in a format string? 6 What's the diffrence between \xFF and 0xFF 3 difference between %0D%0A and /n 9 difference between "\0" and '\0' 3 Is there a difference between %0.f and %.f in C 4 What is the difference between %f and %F in C?

Hot Network Questions

  • Installing a 240V outlet on Aluminum Wiring for an Electric Oven in Old House
  • Is is plausible that we could have neuronal maps of human brains without mind uploading being possible?
  • What's the deal with De Morgan algebras and Kleene algebras?
  • Categories in which isomorphism of stalks does not imply isomorphism of sheaves
  • Can I protect my EV car charger's cable with aluminum tape and a stainless steel hose helix?
  • Are Quantum Algorithms better than classical algorithms for all problems?
  • Is eating onion and garlic sin?
  • Scandinavian film, 1980s, possibly called Royal Toilet?
  • Is Exception caught in the Service class a matter of preference?
  • Is using a virtualised android instance or emulator within "operating system vendor supported or warranted configurations"?
  • Do hypotheses need a “how” explanation or are predictions enough to validate them?
  • Is partial correctness decidable?
  • In what order should I watch the Hunger Games films?
  • What is the best language to speak with locals in Singapore?
  • Can I pretend that Spearman's = Pearson's correlation coefficients for meta analysis?
  • How to handle a campaign where the players are trapped inside a monster controlled by another player?
  • What is the basis for the view of Oneness in Theravadha?
  • Accused of violating NDA on thesis
  • Does the earliest known use of an "average" occur after the invention of calculus?
  • How to find NVMe version on the motherboard?
  • "That right there is why…”
  • Possible bug in RegionDistance when used with Rotate and Translate
  • Understanding Linux 'top' command: Memory vs Swap display format confusion
  • Darlington-driven PNP vs. MOSFET
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-c

Từ khóa » C 0x 08x