Correct format specifier to print pointer or address?

前端 未结 5 1703
自闭症患者
自闭症患者 2020-11-21 13:17

Which format specifier should I be using to print the address of a variable? I am confused between the below lot.

%u - unsigned integer

%x

5条回答
  •  Happy的楠姐
    2020-11-21 14:08

    p is the conversion specifier to print pointers. Use this.

    int a = 42;
    
    printf("%p\n", (void *) &a);
    

    Remember that omitting the cast is undefined behavior and that printing with p conversion specifier is done in an implementation-defined manner.

提交回复
热议问题