<分区>
我需要在中断几年后使用 C++ 进行开发。无论如何,按照我目前正在阅读的教程,我遇到了问题。编写以下代码片段后,我希望在我的控制台中看到“Hello World”,但我只能看到“Debug: StrangeChars”;什么地方出了错?
std::string myString("Hello World");
printf("* Debug: %s \n", myString);
<分区>
我需要在中断几年后使用 C++ 进行开发。无论如何,按照我目前正在阅读的教程,我遇到了问题。编写以下代码片段后,我希望在我的控制台中看到“Hello World”,但我只能看到“Debug: StrangeChars”;什么地方出了错?
std::string myString("Hello World");
printf("* Debug: %s \n", myString);
最佳答案
printf
依赖于您传递正确的参数。 %s
需要一个 char *
,您传递了一个 std::string
。
尝试(C方式)
char myString[] = "Hello World";
printf("* Debug: %s \n", myString);
或者(混合 C/C++ 方式)
std::string myString("Hello World");
printf("* Debug: %s \n", myString.c_str());
或者 C++ 方式:
std::string myString("Hello World");
std::cout << "* Debug " << myString << std::endl;
关于c++ - printf() 令人惊讶的控制台输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18825596/