211

My understanding is that string is a member of the std namespace, so why does the following occur?

#include <iostream>

int main()
{
    using namespace std;

    string myString = "Press ENTER to quit program!";
    cout << "Come up and C++ me some time." << endl;
    printf("Follow this command: %s", myString);
    cin.get();

    return 0;
}

enter image description here

Each time the program runs, myString prints a seemingly random string of 3 characters, such as in the output above.

3
  • 8
    Just to let you know, a lot of people criticize that book. Which I can understand, because there isn't much about object-oriented programming, but I don't think it is as bad as people claim.
    – Jesse Good
    Jun 2, 2012 at 21:27
  • ouf! well, it's good to keep this in mind while i make my way thru the book. I'm sure it won't be the only C++ book i'll read over the course of the next year or so, so i hope it doesn't do too much damange :) Jun 2, 2012 at 21:30
  • Using the highest compiler warning would answer your question - when compiling with gcc. How MSVC handles this - I don't know. Apr 9, 2018 at 20:31

9 Answers 9

321

C++23 Update

We now finally have std::print as a way to use std::format for output directly:

#include <print>
#include <string>

int main() {
    // ...
    std::print("Follow this command: {}", myString);
    // ...
}

This combines the best of both approaches.

Original Answer

It's compiling because printf isn't type safe, since it uses variable arguments in the C sense1. printf has no option for std::string, only a C-style string. Using something else in place of what it expects definitely won't give you the results you want. It's actually undefined behaviour, so anything at all could happen.

The easiest way to fix this, since you're using C++, is printing it normally with std::cout, since std::string supports that through operator overloading:

std::cout << "Follow this command: " << myString;

If, for some reason, you need to extract the C-style string, you can use the c_str() method of std::string to get a const char * that is null-terminated. Using your example:

#include <iostream>
#include <string>
#include <stdio.h>

int main()
{
    using namespace std;

    string myString = "Press ENTER to quit program!";
    cout << "Come up and C++ me some time." << endl;
    printf("Follow this command: %s", myString.c_str()); //note the use of c_str
    cin.get();

    return 0;
}

If you want a function that is like printf, but type safe, look into variadic templates (C++11, supported on all major compilers as of MSVC12). You can find an example of one here. There's nothing I know of implemented like that in the standard library, but there might be in Boost, specifically boost::format.


[1]: This means that you can pass any number of arguments, but the function relies on you to tell it the number and types of those arguments. In the case of printf, that means a string with encoded type information like %d meaning int. If you lie about the type or number, the function has no standard way of knowing, although some compilers have the ability to check and give warnings when you lie.

3
  • @MooingDuck, Good point. It's in Jerry's answer, but being the accepted answer, this is what people see, and they might leave before seeing the others. I've added that option in so as to be the first solution seen, and the recommended one.
    – chris
    Jan 9, 2013 at 1:11
  • Update: std::format is included in C++20, though it's not included in clang at the moment (not sure about gcc) Apr 14, 2021 at 20:17
  • 1
    @OvinusReal, It is, but there's no new function (yet) to print to stdout. std::format alone doesn't actually help here since there's no real formatting going on (cout << "..." << s; vs. cout << format("... {}", s);).
    – chris
    Apr 14, 2021 at 20:36
43

use myString.c_str() if you want a c-like string (const char*) to use with printf

thanks

39

Please don't use printf("%s", your_string.c_str());

Use cout << your_string; instead. Short, simple and typesafe. In fact, when you're writing C++, you generally want to avoid printf entirely -- it's a leftover from C that's rarely needed or useful in C++.

As to why you should use cout instead of printf, the reasons are numerous. Here's a sampling of a few of the most obvious:

  1. As the question shows, printf isn't type-safe. If the type you pass differs from that given in the conversion specifier, printf will try to use whatever it finds on the stack as if it were the specified type, giving undefined behavior. Some compilers can warn about this under some circumstances, but some compilers can't/won't at all, and none can under all circumstances.
  2. printf isn't extensible. You can only pass primitive types to it. The set of conversion specifiers it understands is hard-coded in its implementation, and there's no way for you to add more/others. Most well-written C++ should use these types primarily to implement types oriented toward the problem being solved.
  3. It makes decent formatting much more difficult. For an obvious example, when you're printing numbers for people to read, you typically want to insert thousands separators every few digits. The exact number of digits and the characters used as separators varies, but cout has that covered as well. For example:

    std::locale loc("");
    std::cout.imbue(loc);
    
    std::cout << 123456.78;
    

    The nameless locale (the "") picks a locale based on the user's configuration. Therefore, on my machine (configured for US English) this prints out as 123,456.78. For somebody who has their computer configured for (say) Germany, it would print out something like 123.456,78. For somebody with it configured for India, it would print out as 1,23,456.78 (and of course there are many others). With printf I get exactly one result: 123456.78. It is consistent, but it's consistently wrong for everybody everywhere. Essentially the only way to work around it is to do the formatting separately, then pass the result as a string to printf, because printf itself simply will not do the job correctly.

  4. Although they're quite compact, printf format strings can be quite unreadable. Even among C programmers who use printf virtually every day, I'd guess at least 99% would need to look things up to be sure what the # in %#x means, and how that differs from what the # in %#f means (and yes, they mean entirely different things).
22
  • 11
    @TheDarkIn1978: You probably forgot to #include <string>. VC++ has some oddities in its headers that will let you define a string, but not send it to cout, without including the <string> header. Jun 2, 2012 at 21:39
  • 43
    @Jerry: Just want to point out that using printf is MUCH faster than using cout when dealing with large data. Thus, please dont say that it is useless :D
    – Programmer
    Feb 5, 2013 at 16:00
  • 9
    @Programmer: see stackoverflow.com/questions/12044357/…. Summary: most times that cout is slower, it's because you've used std::endl where you shouldn't. Feb 5, 2013 at 16:55
  • 10
    OK, sorry for the snappy comment. Still, printf is pretty handy for debugging and the streams, though vastly more powerful, have the drawback that the code does not give any idea of the actual output. For formatted output, printf is still a viable alternative, and it's a shame both systems can't cooperate better. Just my opinion, of course.
    – kuroi neko
    Aug 31, 2014 at 15:45
  • 6
    The day you'll have to translate all the output of a large software for i18n, you'll really really hate those who taught you to use cout. cout is dumb, not stateless (provided you use some library that's modifying the cout state, you'll end up with code like this "cout << 64" outputting "40". Code like this cout << "The" << color << "house" is untranslatable and so on. printf does not have any of those design defects.
    – xryl669
    Mar 15, 2020 at 13:10
15

Use std::printf and c_str() example:

std::printf("Follow this command: %s", myString.c_str());
4
3

You can use snprinft to determine the number of characters needed and allocate a buffer of the right size.

int length = std::snprintf(nullptr, 0, "There can only be %i\n", 1 );
char* str = new char[length+1]; // one more character for null terminator
std::snprintf( str, length + 1, "There can only be %i\n", 1 );
std::string cppstr( str );
delete[] str;

This is a minor adaption of an example on cppreference.com

1
  • Seems a bit verbose... May 23, 2022 at 20:19
2

printf accepts a variable number of arguments. Those can only have Plain Old Data (POD) types. Code that passes anything other than POD to printf only compiles because the compiler assumes you got your format right. %s means that the respective argument is supposed to be a pointer to a char. In your case it is an std::string not const char*. printf does not know it because the argument type goes lost and is supposed to be restored from the format parameter. When turning that std::string argument into const char* the resulting pointer will point to some irrelevant region of memory instead of your desired C string. For that reason your code prints out gibberish.

While printf is an excellent choice for printing out formatted text, (especially if you intend to have padding), it can be dangerous if you haven't enabled compiler warnings. Always enable warnings because then mistakes like this are easily avoidable. There is no reason to use the clumsy std::cout mechanism if the printf family can do the same task in a much faster and prettier way. Just make sure you have enabled all warnings (-Wall -Wextra) and you will be good. In case you use your own custom printf implementation you should declare it with the __attribute__ mechanism that enables the compiler to check the format string against the parameters provided.

1

The main reason is probably that a C++ string is a struct that includes a current-length value, not just the address of a sequence of chars terminated by a 0 byte. Printf and its relatives expect to find such a sequence, not a struct, and therefore get confused by C++ strings.

Speaking for myself, I believe that printf has a place that can't easily be filled by C++ syntactic features, just as table structures in html have a place that can't easily be filled by divs. As Dykstra wrote later about the goto, he didn't intend to start a religion and was really only arguing against using it as a kludge to make up for poorly-designed code.

It would be quite nice if the GNU project would add the printf family to their g++ extensions.

1

Here’s a generic way of doing it.

#include <string>
#include <stdio.h>

auto print_helper(auto const & t){
    return t;
}
auto print_helper(std::string const & s){
    return s.c_str();
}
std::string four(){
    return "four";
}
template<class ... Args>
void print(char const * fmt, Args&& ...args){
    printf(fmt, print_helper(args) ...);
}
int main(){
    std::string one {"one"};
    char const * three = "three";
    print("%c %d %s %s, %s five", 'c', 3+4, one + " two", three, four());
}
0

Printf is actually pretty good to use if size matters. Meaning if you are running a program where memory is an issue, then printf is actually a very good and under rater solution. Cout essentially shifts bits over to make room for the string, while printf just takes in some sort of parameters and prints it to the screen. If you were to compile a simple hello world program, printf would be able to compile it in less than 60, 000 bits as opposed to cout, it would take over 1 million bits to compile.

For your situation, id suggest using cout simply because it is much more convenient to use. Although, I would argue that printf is something good to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.