当我使用printf %p
打印内存地址时,我得到十六进制
形式的地址 - 类似于0x7ffee35f5498
。我想知道为什么 printf
返回值为 16
而不是字符串的实际长度,在本例中为 14
?
#include <stdio.h>
int main(void)
{
char *s = "Hello World!";
int x;
x = printf("%p\n", (void*)&s);
printf("%d\n", x);
return (0);
}
output:
0x7ffeea6a3790
16
地址有 14 个字符,但函数返回 16。
文档说:
printf() : It returns total number of Characters Printed, Or negative value if an output error or an encoding error. ...
最佳答案
您不仅打印地址,还打印地址后的换行符。在 Linux/MacOS 系统上,换行符是一个字符 (0xA),而在 Windows 系统上,换行符是两个字符 (0xD 0xA)。
根据您的评论,您说您的输出为 15,并且您使用的是 MacOS。因此,打印地址为 14,换行符为 1,这就是预期的结果。
关于c - printf %p 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59215013/