Internationalization and localization tools


String Format Function

int snprintf(char *buffer, size_t count, const char *format [, argument] ...);

int _snprintf(char *buffer, size_t count, const char *format [, argument] ...);

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

int _snwprintf(wchar_t *buffer, size_t count, const wchar_t *format [, argument] ...);

int _sntprintf(TCHAR *buffer, size_t count, const TCHAR *format [, argument] ...);

Internationalization (I18n) Function Overview

The _snprintf/snprintf function formats and stores a maximum of count characters in buffer, under the control of format and its optional argument data.

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 ANSI swprintf function, which is akin to the Windows _snwprintf function.

The Windows swprintf function on the hand is a wide version of sprintf. If scanning a Windows code base and investigating the swprintf function, this is the wrong help page. Please see Windows swprintf instead.

On Windows platforms, the _snprintf function returns the number of bytes written. On error a negative value is returned.

On ANSI platforms, the snprintf function returns the number of characters written. Prior to GNU C Library 2.1, on error a negative value was returned. For GNU C Library 2.1 and later, the error return of the snprintf function was changed to be compliant with ISO C99 standards. Instead of a negative value return when count is too small, snprintf returns the number of bytes needed for a successful operation, excluding the null-terminating byte. It is up to the caller to check that this value is larger than the space allocated, allowing the function to be called again with a sufficient amount of storage space in buffer.

The Windows _snwprintf function and ANSI swprintf function are the wide character versions of _snprintf/snprintf; their parameters are wide-character strings, and they return the number of wide characters written if successful. On error a negative value is returned.

_sntprintf is the Windows-only Generic version of the function; with the _MBCS or _UNICODE compiler flags determining its mapping to either snprintf or _snwprintf.

I18n Issues

If scanning a Windows code base and investigating the swprintf function, this is the wrong help page. Please see Windows 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.

Special care must be taken with the count parameter. See Locale-Sensitive Length Functions for a complete discussion of the issues involved with functions that pass length parameters.

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