How can I use _stprintf in my programs with and without UNICODE support?

Microsoft <tchar.h> defines _stprintf as swprintf if _UNICODE specified, and sprintf if not. But these functions take different arguments! In swprintf second argument is the size of the buffer, but sprintf does not have this.

Has anyone gone? If so, this is a big question. How to use _stprintf in my programs and work with them without _UNICODE ?

+4
source share
3 answers

Here you see a parallel evolution. swprintf is late to the C standard after it was discovered that (A) 8 bits is not enough for the text, and (B) you must pass the buffer sizes along with the buffers. TCHAR is Microsoft's idea for unifying ASCII and Unicode APIs. They dropped the ball, missing the point (B). The correct TCHAR solution should have been to define _stprintf as either swprintf or snprintf .

Then the solution should simply wrap <tchar.h> and do it yourself.

+5
source

these functions take different arguments!

Two versions are available in MS compilers. Take a look here . This complies with the ANSI standard. But I think this does not answer your question. I will skip it for a while and soon tell you how you can have uniformity.

make them work with and without _UNICODE?

You are better off using "safe string functions" as recommended by MS. See this . Use `_stprintf_s' and I think you will get around your problem.

Has anyone gone?

EDITED: I don't think so. I do not have sufficient justification to give you an answer. I will post the update when I get something more specific. At the same time, look at the explanations of MSalters.

Curiously, MS C runtime does not claim to be ISO compliant.

Disclaimer: I am not defending the giant Redmond, only pointing out things that seem strange to me!

+1
source

This may not be the direct answer to the question, but one option is to use _stprintf_s . You must add additional parm, but then it will still compile in both directions and will be more reliable for the future.

0
source

All Articles