#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
int cnt;
char buf[100];

cnt = printf("Hello world!\n");
printf("printf ret: %d\n", cnt);
printf("strlen: %d\n", strlen("Hello world!\n"));

memset(buf, 0, sizeof(buf));
cnt = sprintf(buf, "%d", 12345);
printf("sprintf ret: %d\n", cnt);

return 0;
}


程序输出:

[root@localhost ~]# ./a.out

Hello world!

printf ret: 13

strlen: 13

sprintf ret: 5

[root@localhost ~]#

(1)二者的返回值为输出的信息的字符个数。包括特殊字符,如控制字符,空格。

可以看到printf返回输出字符个数,跟strlen返回字符串长度相等。