printf能输出string型的变量吗?

来源:百度知道 编辑:UC知道 时间:2024/06/01 18:29:22
我试过了这样会出现运行错误的
我用的工具是是DEV-CPP(版本是4.9.9.2),是否与所用工具有关啊?

实践证明是不行的(我用的也是Dev-C++)。理论上也讲不通,string是类,printf()只能打印基本类型。本来就不能这样做,找本书好好读读。
应该用
string s;
cout << s;

printf("%s", s.c_str()); //不推荐

string m
printf("%s",m);

当然能,用%s传参

同意。