?C++?,printf
????C???????,????????printf
????????????????:
- ????:
printf
?????????????????(format string),?????????????????????????????????(placeholder)?????
#includeusing namespace std; int main() { int a = 5; float b = 7.5; const char* s = "Hello, World!"; printf("Integer: %d, Float: %.2f, String: %s\n", a, b, s); return 0; }
- ????:???????????????????????????????????????????:
%d
:??(int)%f
:???(float/double)%s
:???(const char*)%c
:??(char)%x
/%X
:??????(int)%o
:?????(int)%e
/%E
:???????????(float/double)%g
/%G
:??????????????(float/double)
????????????,??:
-
:???+
:?????0
:???????
:??????,??????.
:?????????*
:????????????
#includeusing namespace std; int main() { int a = -123; float b = 3.14159; printf("Left aligned: %-10d\n", a); printf("Always show sign: %+d\n", a); printf("Leading zeros: d\n", a); printf("Space for positive numbers: % d\n", a); printf("Fixed decimal places: %.2f\n", b); printf("Variable width and precision: %*.*f\n", 8, 2, b); return 0; }
??:?C++?,?????std::cout
???????,???????????????????printf
???C++?????,??????????