printf with std::string?
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.
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:
- 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. -
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. -
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 like123.456,78
. For somebody with it configured for India, it would print out as1,23,456.78
(and of course there are many others). Withprintf
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 toprintf
, becauseprintf
itself simply will not do the job correctly. - Although they're quite compact,
printf
format strings can be quite unreadable. Even among C programmers who useprintf
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).
use myString.c_str()
if you want a c-like string (const char*
) to use with printf
thanks
Use std::printf and c_str() example:
std::printf("Follow this command: %s", myString.c_str());