640printf()ing string in C++

Silly, little C++ Beginner’s mistake.

How do you print strings in C++?

string test = "tssst";
printf(test);

Of course not, that’s C++!!!

That’s how you do it:

printf(test.c_str());

And if you want to stop the compiler throwing warnings:

printf("%s", test.c_str());

So close, yet so far out.

452Padding with Zeros (or other characters)

int x = 11;
 
[NSString stringWithFormat:@"%03i", x];
// @"011"
 
[NSString stringWithFormat:@"%+5i", x];
// @"+++11"
 
[NSString stringWithFormat:@"%+05i", x];
// @"+0011"

And not really like that. 381. I’ll still have to learn a lot of C.

305Converting float to string %4.2f

printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);

floats: 3.14 +3e+000 3.141600E+000