0

This code will print my_age and memory address of my age.

#include <stdio.h> 
#include <stdlib.h> 

int main(){ 

int my_age = 24;        
printf("%d \t %p",my_age, &my_age); 
}

But when i use %d instead of %p it will print a different value what is that value ??

#include <stdio.h> 
#include <stdlib.h> 

int main(){ 

int my_age = 24;        
printf("%d \t %d",my_age, &my_age); 
}
  • %p is a specificator for pointer address – user 7362383 Apr 03 '17 at 21:54
  • 6
    If you use the wrong format specifier, ub is invoked. Your program could do literally [anything](https://en.wikipedia.org/wiki/Undefined_behavior) – George Apr 03 '17 at 21:55
  • 1
    Your compiler should give a warning! If not, use another! – dtell Apr 03 '17 at 22:00
  • 1
    `printf("%d \t %p",my_age, &my_age); ` should be `printf("%d \t %p",my_age, (void*)&my_age); ` but asking why beans are not carrots is nonsense. Perhaps you have 32-bit `int` and 64-bit `int*`. – Weather Vane Apr 03 '17 at 22:00
  • Did you *expect* it print the same result when you used a different format specifier? I can't imagine why you would do. – John Bollinger Apr 03 '17 at 22:04
  • Summary: stop doing wrong things and you will not feel the need to have wrong things explained. – ThingyWotsit Apr 03 '17 at 22:28

2 Answers2

6

When you compile your code with %d being used for a pointer like &my_age, you should get a warning similar to:

printf.c:10:1: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]

This is because you are using the wrong format option, and this results in unspecified behavior. The results are architecture and implementation dependent. In other words, unpredictable. Don't do that.

On my computer for example, the %d expects a 4 byte value, while an address like &my_age is a 8 byte value. On my computer, this results in printf displaying the least significant 4 bytes of the pointer (which has a 1 in its msbit, so is interpreted as a negative 4 byte integer).

#include <stdio.h> 
#include <stdlib.h> 

int main(){ 

int my_age = 24;        
printf("sizeof(my_age)=%d, sizeof(&my_age)=%d\n", sizeof(my_age), sizeof(&my_age));
printf("%d \t %p\n",my_age, &my_age); 
printf("%d \t %d\n",my_age, &my_age); 
}

Results in output:

sizeof(my_age)=4, sizeof(&my_age)=8
24   0x7ffdf27cb52c
24   -226708180

The least significant 4 byte hex value of the pointer is f27cb52c and is interpreted as decimal -226708180.

Since specifics are architecture dependent, your results may vary. See post here for more details.

Bottom line: ensure that all warnings are resolved before testing your code.

Community
  • 1
  • 1
ScottK
  • 1,526
  • 1
  • 16
  • 23
  • `%d` expects an `int`. That is not necessarily a 4-byte value -- it can and does depend on the implementation. Similarly, pointers are not necessarily 8 bytes. That size is common on 64-bit systems, but by no means required. The actual size could be either larger or smaller. – John Bollinger Apr 03 '17 at 22:06
  • @JohnBollinger: Absolutely agree. I clarified that 4 bytes and 8 bytes were specific to my testbed, and other's mileage may vary. – ScottK Apr 03 '17 at 22:08
2

%p is displaying the address in hex and

%d is displaying the address in decimal

%x will display it in hex

The % specifiers that you can use in ANSI C are:

Usual variable type Display

%c char single character
%d (%i) int signed integer
%e (%E) float or double exponential format
%f float or double signed decimal
%g (%G) float or double use %f or %e as required
%o int unsigned octal value
%p pointer address stored in pointer
%s array of char sequence of characters
%u int unsigned decimal
%x (%X) int unsigned hex value

Reference

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
dmaelect
  • 151
  • 2
  • 14
  • 1
    The only problem is that using anything except `%p` for a pointer is invoking undefined behavior. – Eugene Sh. Apr 03 '17 at 22:16
  • To add to [@EugeneSh.](http://stackoverflow.com/questions/43194943/what-does-d-print-for-variable-instead-of-p-in-c#comment73463183_43195184): UB aside, `int` or `unsigned` may not be able to hold all addresses. `%d` expects `int`s, and `%x` expects `unsigned int`s. – ad absurdum Apr 03 '17 at 22:59