C++ swprintf()

The swprintf() function in C++ is used to write a formatted wide string to a wide string buffer.

The swprintf() function is defined in <cwchar> header file.

swprintf() prototype

int swprintf( wchar_t* buffer, size_t size, const wchar_t* format, ... );

The swprintf() function writes the wide string pointed to by format to the buffer. The maximum number of characters that can be written is (size-1).

After the characters are written, a terminating null wide character is added.

The wide string format may contain format specifiers starting with % which are replaced by the values of variables that are passed to the swprintf() function as additional arguments.


swprintf() Parameters

  • buffer: Pointer to the string buffer to write the result.
  • size: Specify maximum number of characters to be written to buffer which is size-1.
  • format: A pointer to a null terminated wide string that is written to stdout. It consists of wide characters along with optional format specifiers starting with %. The format specifiers are replaced by the values of respective variables that follows format.
    The format specifier has the following parts:
    • A leading % sign
    • Flags: Optional one or more flags that modifies the conversion behavior.
      • - : Left justify the result within the field. By default it is right justified.
      • + : The sign of the result is attached to the beginning of the value, even for positive results.
      • Space: If there is no sign, a space is attached to the beginning of the result.
      • # : An alternative form of the conversion is performed.
      • 0 : It is used for integer and floating point number. Leading zeros are used to pad the numbers instead of space.
    • Width: An optional * or integer value used to specify minimum width field.
    • Precision : An optional field consisting of a . followed by * or integer or nothing to specify the precision.
    • Length : An optional length modifier that specifies the size of the argument.
    • Specifier: A conversion format specifier. The available format specifiers are as follows:
    Format specifiers
    Format Specifier Description
    % Prints %
    c Writes a single character
    s Writes a character string
    d or i Converts a signed integer to decimal representation
    o Converts an unsigned integer to octal representation
    X or x Converts an unsigned integer to hexadecimal representation
    u Converts an unsigned integer to decimal representation
    F or f Converts floating-point number to the decimal representation
    E or e Converts floating-point number to the decimal exponent notation
    A or a Converts floating-point number to the hexadecimal exponent
    G or g Converts floating-point number to either decimal or decimal exponent notation
    n Returns the number of characters written so far by this call to the function. The result is written to the value pointed to by the argument
    p Writes an implementation defined character sequence defining a pointer.

    So the general format of format specifier is:
    %[flags][width][.precision][length]specifier
  • … : Other additional arguments specifying the data to be written. They occur in a sequence according to the format specifier.

swprintf() Return value

  • On success, the swprintf() function returns number of characters written excluding the terminating null wide character.
  • If an encoding error occurred or if the number of characters to be generated was equal or greater than size, a negative value is returned.

Example: How swprintf() function works?

#include <cwchar>
#include <cstdio>
#include <clocale>
#include <iostream>
using namespace std;

int main()
{
	wchar_t hebrew_str[] = L"\u05D0 \u05D1 \u05E1 \u05D3 \u05EA";
	wchar_t buffer[100];

	setlocale(LC_ALL, "en_US.UTF-8");
	swprintf(buffer, sizeof(hebrew_str), L"%ls", hebrew_str);

	wcout << L"Some hebrew letters\n" << buffer << endl;
	return 0;
}

When you run the program, the following will be written to example.txt:

Some hebrew letters
א ב ס ד ת