“printf” on strings prints gibberish

前端 未结 4 1877
执笔经年
执笔经年 2020-12-06 17:05

I\'m trying to print a string the following way:

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

but all I

4条回答
  •  感情败类
    2020-12-06 17:22

    Because %s indicates a char*, not a std::string. Use s.c_str() or better still use, iostreams:

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

提交回复
热议问题