Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use wsprintf() to print a double as a wide string?

I am unable to print double value using wsprintf(). I tried sprintf() and it worked fine.

Syntax used for wsprintf() and sprintf() is as follows:

wsprintf(str,TEXT("Square is %lf "),iSquare); // Does not show value

sprintf(str," square is %lf",iSquare);  // works okay

Am I making any mistakes while using wsprintf() ?

like image 739
void Avatar asked Aug 12 '13 20:08

void


3 Answers

wsprintf doesn't support floating point. The mistake is using it at all.

If you want something like sprintf, but for wide characters/strings, you want swprintf instead.

Actually, since you're using the TEXT macro, you probably want _stprintf instead though: it'll shift from a narrow to wide implementation in sync with the same preprocessor macros as TEXT uses to decide whether the string will be narrow or wide. This whole approach, however, is largely a relic from the days when Microsoft still sold and supported versions of Windows based on both the 32-bit NT kernel, and on the 16-bit kernel. The 16-bit versions had only extremely minimal wide-character support, so Microsoft worked hard at allowing a single source code base to be compiled to use either narrow characters (targeting 16-bit kernels) or wide characters (to target the 32-bit kernels). The 16-bit kernels have been gone for long enough that almost nobody really has much reason to support them any more.

For what it's worth: wsprintf is almost entirely a historic relic. The w apparently stands for Windows. It was included as part of Windows way back when (back to the 16-bit days). It was written without support for floating point because at that time, Windows didn't use any floating point internally--this is part of why it has routines like MulDiv built-in, even though doing (roughly) the same with floating point is quite trivial.

like image 184
Jerry Coffin Avatar answered Oct 20 '22 11:10

Jerry Coffin


The function wsprintf() does not support floating point parameters, try using swprintf() instead if you're working with floating point values.

More information about swprint can be found here

like image 29
Nexuz Avatar answered Oct 20 '22 13:10

Nexuz


wsprintf does not support floating point. See its documentation - lf is not listed as a valid format code.

The swprintf function part of the Visual Studio standard library is what you want. It supports all of the format codes that sprintf does.

like image 35
shf301 Avatar answered Oct 20 '22 13:10

shf301