Use the format specifier %p:
printf(“variable A is at address: %pn”, (void*)&A);
The standard requires that the argument is of type void* for %p specifier. Since, printf is a variadic function, there’s no implicit conversion to void * from T * which would happen implicitly for any non-variadic functions in C. Hence, the cast is required. To quote the standard:
7.21.6 Formatted input/output functions (C11 draft)
p The argument shall be a pointer to void. The value of the pointer is
converted to a sequence of printing characters, in an
implementation-defined manner.
Whereas you are using %x, which expects unsigned int whereas &A is of type int *. You can read about format specifiers for printf from the manual. Format specifier mismatch in printf leads to undefined behaviour.
A workaround to use %x with length specifier to print an int or unsigned int without compiler complaining about casting would be to use malloc:
unsigned int* D = malloc(sizeof(unsigned int)); // Allocates D
unsigned int D_address = *((unsigned int*) &D); // D address for %08x without warning
*D = 75; // D value
printf(“variable D is at address: %p / 0x%08x with value: %un”, D, D_address, *D);
Alternatively you can compile with gcc -w flag to suppress those casting warnings.
Edit for 64-bit addresses:
unsigned long* D = malloc(sizeof(unsigned long)); // Allocates D
unsigned long D_address = *((unsigned long*) &D); // D address for %016lx without warning
*D = ULONG_MAX; // D value
printf(“variable D is at address: %p / 0x%016lx with value: %lun”, D, D_address, *D);