The Linux Page

wsprintf() limit under MS-Windows

Today I wanted to check out a set of floating value in my Visual C++ development environment.

You can use the OutputDebugString() to print a debug string in the VC++ output window when running a software. This is quite handy, but when working in C++, string handling can be tedious. I need to create a message and wrote something similar to this:

wchar_t msg[128];
swprintf(msg, "Float: %f\n", my_float);
...

That code behaved strangely: it would remove the % and print f instead of replacing the %f as the value of the float.

I tried several times and the fact is that the swprintf() under MS-Windows does not work at all with floating points. This sounds real weird, but you can see as per the description that the floats are not included in the format, and thus the %f is ignored.,

Note that if you are using Qt there is a better method: qDebug(). This is similar to a printf() but will send the output in your debug output window and the console. That's done internally, so you don't even have to call OutputDebugString() and thus no need to swprintf(). Also, you can create a QDebug object that sends the output to qDebug and in that case you can send strings, integers, etc. in a way similar to cout or cerr: using the << operator.