What's the proper use of printf to display pointers padded with 0s

后端 未结 9 2143
暖寄归人
暖寄归人 2021-01-31 14:32

In C, I\'d like to use printf to display pointers, and so that they line up properly, I\'d like to pad them with 0s.

My guess was that the proper way to do this was:

9条回答
  •  Happy的楠姐
    2021-01-31 15:18

    As your link suggests already, the behaviour is undefined. I don't think there's a portable way of doing this as %p itself depends on the platform you're working on. You could of course just cast the pointer to an int and display it in hex:

    printf("0x%016lx", (unsigned long)ptr);
    

提交回复
热议问题