1

I'm trying to print a memory address in C, this is the code I'm using

#include <stdio.h> int main() { int v = 10; printf("Address of the v: %p\n",&v); return 0; } 

The output is supposed to start with 0x, but I don't have this thing, it shows this 0061FF14 I am using MingW32 gcc compiler I changed it to 64 and when I typed the code again the output is 000000A57A7FF67C

When I use https://www.onlinegdb.com/ the output is normal with 0x

My machine is 64 bit I am using vscode editor, how do I make the address appear as normal 0x

6
  • 9
    The output of the p specifier is implementation defined. It's up to the implementation whether to include the 0x or not. Commented Aug 13, 2023 at 0:29
  • "The output is supposed to start with 0x" Who did tell you that? Commented Aug 13, 2023 at 0:32
  • 4
    %p is largely intended for debugging. Addresses within a process are generally not even portable to other processes of the same system, let alone other systems, and that is why the format is left as implementation-defined rather than more fully specified by the C standard. If you want finer control over the formatting, you can convert an address to the uintptr_t type (defined in <stdint.h> and use the fuller format modifications available with the integer types, including using the PRIxPTR macro for the format defined in <inttypes.h>. Commented Aug 13, 2023 at 2:07
  • 2
    If you want to control the format of printed addresses, use #include <inttypes.h> and printf("0x%" PRIXPTR "\n", (uintptr_t)pointer);. For myself, I prefer hex to be printed with 0xABCD0123EF456789 with the dip at the start for the x and the rest all full height. If you prefer lower-case alpha characters in your hex, use PRIxPTR instead. You can specify how many digits to print using, for example, printf("0x%.12" PRIXPTR "\n", (uintptr_t)pointer)). Commented Aug 13, 2023 at 5:46
  • @JonathanLeffler does standard guarantee that %p will produce the same result as printing pointer converted to integer? Commented Aug 13, 2023 at 9:07

1 Answer 1

2

Why does 0x not appear when I print a memory address in C?

Because it is not specified by the standard. The implementation can choose to print it or not.

  1. Your code invokes undefined behaviour as the pointer has to have type pointer to void (void *).
  2. If you your implementation does not print "0x" simply add it to the format string.
 printf("Address of the v: 0x%p\n",(void *)&v); 

or if your implementation supports it

 printf("Address of the v: %#p\n",(void *)&v); 
Sign up to request clarification or add additional context in comments.

3 Comments

Not the DV, I was about to comment that you never need to cast to (void*) as that is implicit, but now I found out there is one exception and that is %p in printf, well learn something new everyday.
Note that the C standard says (for the printf() # modifier): # The result is converted to an ''alternative form''. For o conversion, … For x (or X) conversion, … For a, A, e, E, f, F, g, and G conversions, … For other conversions, the behavior is undefined.. The p conversion specifier is one of the 'other conversions' — using %#p is undefined behaviour. Obviously, a platform may define the behaviour, but you have to establish that before using it in production code.
@Fredrik, the cast is required when used in any varargs function; the printf family is merely the most frequently encountered of these.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.