将 printf() 结果分配给变量

时间:2021-03-15 03:30:38

标签: c printf

当我在没有 \n 的情况下运行此代码时,输​​出为 hello5,但使用 \n 时,输出为 hello6,有人可以解释一下吗? >

#include <stdio.h>

//Compiler version gcc  6.3.0

int main()
{
  int c;
  c = printf("hello\n");
  printf("%d",c);
  return 0;
}

1 个答案:

答案 0 :(得分:0)

printf 函数返回打印的字符总数。

当你打印 "hello\n" 时,它是 6 个字符,所以 c 是 6。当你打印 "hello" 时,它是 5 个字符,所以 c 是 5。