is defined in terms of the C standard library, which doesn't know anything about C++ strings. So there's no format specifier for C++ strings.
It's usually best to use cout anyways, because that interface avoids a number of significant problems with printf. Unfortunately, this added safety is bought at the expense of brevity. If you need to do complicated string formatting that would be too difficult or unreadable with cout, you could look into another library, like Boost.format.
|"my value" has a memory address?|
Surprisingly, yes! Good observation.
Among all literals, only string literals (stuff in double quotes) have addresses. The (narrow string) literal itself is an array of type char const
. In the jargon, we'd say that string literals are a kind of lvalue
(Note: this exposes an error - the statement
char *str = "my value"
will not compile; it throws away the
. Many compilers do this as an extension for compatibility with C, but it is illegal regardless.)
Assuming we modify your example to compile:
char const *str = "my value";
Then the array "my value" undergoes the array-to-pointer conversion, yielding a pointer to the first element of the string literal (the 'm'), and that pointer is used to initialize str
. String literals have static storage duration