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

Sovereign
  • 11
  • 1
  • 9
    The output of the `p` specifier is implementation defined. It's up to the implementation whether to include the `0x` or not. – Christopher Moore Aug 13 '23 at 00:29
  • *"The output is supposed to start with 0x"* Who did tell you that? – 0___________ Aug 13 '23 at 00: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 `` and use the fuller format modifications available with the integer types, including using the `PRIxPTR` macro for the format defined in ``. – Eric Postpischil Aug 13 '23 at 02:07
  • 2
    If you want to control the format of printed addresses, use `#include ` 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)`). – Jonathan Leffler Aug 13 '23 at 05:46
  • @JonathanLeffler does standard guarantee that %p will produce the same result as printing pointer converted to integer? – 0___________ Aug 13 '23 at 09:07
  • @0___________ ————— No – Jonathan Leffler Aug 13 '23 at 14:02

1 Answers1

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);
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 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. – Fredrik Aug 13 '23 at 01:29
  • 1
    Note that the C standard says (for the [`printf()`](http://port70.net/~nsz/c/c11/n1570.html#7.21.6.1) `#` 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. – Jonathan Leffler Aug 13 '23 at 05:36
  • 1
    @Fredrik, the cast is required when used in any _varargs_ function; the `printf` family is merely the most frequently encountered of these. – Toby Speight Aug 13 '23 at 09:50