简体   繁体   中英

printf %p return value

When I print a memory address with printf %p I get address in hexdecimal - something like 0x7ffee35f5498 . I am wondering why the printf return value is 16 and not the actual length of string, in this case 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

Address has 14 char but the function returned 16.

Documentation say's:

printf() : It returns total number of Characters Printed, Or negative value if an output error or an encoding error. ...

You're not just printing the address but also a newline after it. On Linux / MacOS systems a newline is one character (0xA) while on Windows systems it is two characters (0xD 0xA).

From your comments, you say you got 15 as your output and that you're on MacOS. So that's 14 for the printed address plus 1 for the newline which is the expected result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM