printf函数配合%s占位符输出字符串类结果是乱码。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string name = "uzi";
printf("name=%s\r\n", name);
system("pause");
return 0;
}
输出结果:

解决方法:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string name = "uzi";
/*printf("name=%s\r\n", name);*/
printf("name=%s;\r\n", (name).c_str());
system("pause");
return 0;
}
验证结果:

原理是啥?有知道的的麻烦评论区告知下😀~
文章讨论了在C++中使用printf函数时遇到的字符串乱码问题,通过将`%s`替换为`(name).c_str()`解决了输出问题。作者寻求了解输出乱码的原理并邀请读者分享知识。
8399

被折叠的 条评论
为什么被折叠?



