gpt4 book ai didi

c++ - "printf"字符串打印乱码

转载 作者:IT老高 更新时间:2023-10-28 22:32:11 31 4
gpt4 key购买 nike

我正在尝试通过以下方式打印字符串:

int main(){
string s("bla");
printf("%s \n", s);
.......
}

但我得到的只是这些随机的胡言乱语。

你能解释一下原因吗?

最佳答案

因为 %s 表示 char*,而不是 std::string。使用 s.c_str() 或更好的仍然使用,iostreams:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string s("bla");
std::cout << s << "\n";
}

关于c++ - "printf"字符串打印乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3634766/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com