C++??printf
????C???????,??????????C++?,??printf
??,?????????????????std::cout
,???????,printf
????????????
????
- ?C???:???C++??C????,??C??????????,???????
printf
? - ??????:????????????,
printf
???std::cout
??,??????????,??????????????? - ??????:??????????????????,
printf
??????????? - ????:??????,
printf
?????????????????????
????
??1:????
#include#include int main() { int a = 10; double b = 3.14; const char* s = "Hello, World!"; // ??printf?? std::printf("a = %d, b = %f, s = %s\n", a, b, s); return 0; }
??2:??????
#include#include int main() { int year = 2023; int month = 9; int day = 15; // ??printf?????? char date[20]; std::snprintf(date, sizeof(date), "%d-d-d", year, month, day); std::cout << "Formatted date: "<< date<< std::endl; return 0; }
??3:?C???
#include
#include
#include
// ??????C???,???????
extern "C" void print_formatted(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
int main() {
int x = 10;
int y = 20;
// ??C???,??printf???????
print_formatted("x = %d, y = %d\n", x, y);
return 0;
}
??????,print_formatted
???C???,???????????C++??C???????,???????C++???va_list
?va_start
?va_end
?????,??????vprintf
??,??????????
????,???C++???????std::cout
????,????????,printf
?????????????C??????????????????????????,printf
???????????