-3

can anybody explain briefly how exactly these specifiers are working..i searched a lot about them but still confused about them.(i have a 32 bit pc).

image of output

#include<stdio.h>

int main()
{
    int v,*p;
    v=3;
    *p=v;
    printf("the value of  v = %d \n",v);
    printf("the address of  v = %d \n\n",&v);

    printf("the value of  v = %p \n",v);
    printf("the address of  v = %p \n\n",&v);

    printf("the value of  v = %u \n",v);
    printf("the address of  v = %u \n\n",&v);

    printf("the value of  v = %x \n",v);
    printf("the address of  v = %x \n\n",&v);

    printf("value stored through p  = %d \n",*p);
    printf("address of p = %d \n\n",p);

    printf("value stored through p  = %p \n",*p);
    printf("address of p = %p \n\n",p);

    printf("value stored through p  = %u \n",*p);
    printf("address of p = %u \n\n",p);

    printf("value stored through p  = %x \n",*p);
    printf("address of p = %x \n\n",p);


    return 0;
}
Simson
  • 3,373
  • 2
  • 24
  • 38
  • The output for `%p` is implementation defined and the type of the corresponding argument must be `void*`. Most lines in this code will cause undefined behaviour. – M. Nejat Aydin Jul 08 '20 at 04:15
  • Most of this is undefined behaviour. The language definition specifies that you have to use the right specifier for the type of the corresponding argument, otherwise it is undefined . – M.M Jul 08 '20 at 04:19
  • Could you please replace the screenshoot I added of the output with a cut and paste from the actual text, it will be easier to read and index the question that way. – Simson Jul 08 '20 at 04:19
  • Ok guys i get that what you want to say but there is an another confusion in my mind that how in this code...int a = 10; int *b = &a; printf("%p\n",b); printf("%X\n",b); ...there are exact two zeros before the %p value i.e-0018FF20 but in %x(i.e-for hexadecimal) the value is simply 18FF20.....i mean what's the logic behind this concept.. – Pranshu taneja Jul 08 '20 at 07:29

1 Answers1

1

%d and %u are for printing numbers in a normal decimal format. Unless you add the options for leading zeroes, e.g. %05d to print at least 5 digits with leading zeroes, it prints in the most compact format, since that's how humans usually prefer to see their numbers.

%p is for printing memory addresses. This is printed in an implementation-dependent format. Since it's usually intended for use by programmers, hexadecimal is the most common format. And it's also usually easiest to deal with them if they're always the same length, so the maximum size for the computer architecture is shown.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ok i get that ..but there is an another confusion;how in this int a = 10; int *b = &a; printf("%p\n",b); printf("%X\n",b); ..code there are exact two zeros before the %p value i.e-0018FF20 but in %x(i.e-for hexadecimal) the value is simply 18FF20.....i mean whats the logic behind this concept.. – Pranshu taneja Jul 08 '20 at 07:24
  • @Pranshutaneja: the conversion specifiers for integer types don’t display leading zeros unless you add a field width and leading zero flag - `%08x` formats a value as hex with a minimum field width of 8 and pads with leading zeros. The behavior of the `%p` specifier depends on the implementation - some implementations pad with leading zeros, some don’t. And to repeat a point made earlier, pointers are not integers, and the behavior of using integer conversion specifiers on pointer values is undefined - you can’t trust the output to be correct. – John Bode Jul 08 '20 at 14:25