   wsprintf
  Hi there I want to format some strings in C++, and getting some troubles, what's wrong in the following code?TCHAR szbuff[100];int il;UINT u=9;il = wsprintf (szbuff, TEXT("the number is: %u", u));TextOut (hdc, 20,30, szbuff, il);I tried many combination of variable type and various format %i, %l, etc..but everytime i get a strange number like 4126578984.What's the best method for composing strings ?thx   
  This happened to me, too! And it took a long time to find the error.Don't use wsprintf use swprintf ;)   ;Quote:This happened to me, too! And it took a long time to find the error.Don't usewsprintfuseswprintf;)This isn't great advice, given the rest of the code provided: swprintf takes wchar_t; but the code in question uses TCHAR, TEXT and other ASCII/Unicode-agnostic typedefs and macros, which means that the code would fail rather quietly in the future when the gating conditions for the evaluation of those macros change (i.e., switching between ASCII and Unicode). On the other hand, wsprintf takes LPTSTR and thus should function correctly.As for the actual problem, have you stepped through with a debugger, Aju? In particular pay careful attention to the value of variables in question before and after the wsprintf() call. Check and see if szbuff contains what you expect Also, a better way to compose strings like this is with the C++ std::stringstream or std::wstringstream classes, and std::string or std::wstring as appropriate (you'd need to build your own "T" typedefs for these types, however, to avoid running afoul of the aforementioned issue).   ;  Dear MITB thx for your replay but i've tried also the swprintf, with the same results.I checked with the debugger, and the variable has the correct value before and after, and also the buffer has the value shown, so seems as the function is working not properly, but i'm pretty sure i'm doing some error that i can't find   ;  The problem is where you've put your brackets. Try this:TCHAR szbuff[100];int il;UINT u=9;il = wsprintf (szbuff, TEXT("the number is: %u"), u);TextOut (hdc, 20,30, szbuff, il);Note the ", u" is now outside of the brackets for the TEXT macro.   ;  I guessed it was an idiot error, thx adam ;)By the way do you suggest too to use std::string, etc... c++ classes ?   ;Quote:Original post by AjuI guessed it was an idiot error, thx adam ;)By the way do you suggest too to use std::string, etc... c++ classes ?Yes. I don't know why you'd be using char or wchar_t arrays in C++, or any of the C library functions to manipulate those. C++ has the string and stringstream classes. There is really no reason not to use them. Sometimes I see C programmers trying to use C++ who are reluctant to do things the C++ way. Their "C++" code is riddled with C function calls to printf, sprintf, ect. IMO this is the wrong way.   ;  To handle Unicode in C++, you would want to use the versions of those classes based on wchar_t: thus std::wstring, etc. However, this is still only a partial solution. Unicode is not easy, people. You will probably want/need third-party library support to handle it properly. Sorry.You should also read this. (Do note that the article is almost 7 years old but people still seem to have difficulty with a lot of these concepts.)   