Internationalization and localization tools


String Format Function

int sprintf(char *buffer, const char *format [, argument] ...

int swprintf(wchar_t *buffer, const wchar_t *format [, argument] ...

int _stprintf(TCHAR *buffer, const TCHAR *format [, argument] ...

Internationalization (I18n) Function Overview

The sprintf function writes output to buffer under control of the format argument. A null character is placed at the end of the string and the number of single-byte characters written is returned, or -1 if an error occurs.

The swprintf function is unusual in that the ANSI version and the Windows version of swprintf are actually different functions. This help page documents the Windows swprintf function, which is a wide version of sprintf.

The ANSI swprintf function on the other hand is more akin to the Windows _snwprintf function. If scanning an ANSI code base and investigating the swprintf function, this is the wrong help page. Please see ANSI swprintf instead.

_stprintf is the Windows-only Generic version of the function; with the _MBCS or _UNICODE compiler flags determining its mapping to either sprintf or swprintf.

On Windows platforms, swprintf is the wide character version of the function and returns the number of wide characters written.

On ANSI platforms, technically speaking there is no wide version of this function. This is because ANSI does support a function called swprintf, however it acts more like snprintf in that it requires a count parameter containing the number of characters that can be printed.

I18n Issues

If scanning an ANSI code base and investigating the swprintf function, this is the wrong help page. Please see ANSI swprintf instead.

Use the appropriate version of the function as required for internationalization support, noting the following:

Prior to calling one of these functions, ensure that the current locale is set properly by calling setlocale, as string formatting is dependent on the LC_NUMERIC locale category. See String Formatting in C and C++ for a discussion on locale-sensitive formatting in internationalized applications.

There is no ANSI wide character version of this function, per se. You can use the ANSI swprintf function, but note you will now have to pass an additional count parameter.

Recommended Replacements*

On Windows platforms, a more secure string function should be used. These are shown as the first choices in the following table:

*If you're already using the recommended function, see I18n Issues for other reasons why Globalyzer is detecting the function.

String Format Functions

 

Lingoport internationalization and localization services and software