Printf函数的返回值是打印的所有字符的总数(包括转义字符)
举几个例子
1. 打印一个数
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 printf("The value of Printf:%d",printf("%d",1)); 8 return 0; 9 }
打印的字符数为1,所以要返回一个字符。
2. 打印多个数
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 printf("The value of Printf:%d",printf("%d,%d,%d",1,23,456)); 8 return 0; 9 }
这次需要打印1,23,456,连标点一共是8个字符,所以返回值是8.
3. 带填充的情况
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 printf("The value of Printf:%d",printf("%05d",1)); 8 return 0; 9 }
这一次连填充一个需要打印5个字符,返回值为5
4. 打印转义字符
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 printf("The value of Printf:%d",printf("\n")); 8 return 0; 9 }
打印换行符和其他字符没有区别,依然返回的是打印字符的个数。