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 }
打印換行符和其他字符沒有區別,依然返回的是打印字符的個數。