In C, swprintf() formats wide text into a buffer. It belongs to the wide-character tools declared in <wchar.h>.
Use wchar_t for wide characters and prefix wide literals with L, as in L"Hello". The examples below use simple C Programming Tutorial patterns and print wide strings with %ls.
Syntax
The following concrete call shows the argument shape before the formal declaration.
C
swprintf(out, 32, L"%d", 42);
Call
What it does
swprintf(out, 32, L"%d", 42)
Formats wide text into a buffer in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
Library declaration
int swprintf(wchar_t *restrict ws, size_t n, const wchar_t *restrict format, ...);
Parameters
Parameter
Description
ws
A wide-string buffer, source string, or format string.
n
The buffer capacity or maximum number of elements to process.
format
A wide format string that controls printing or scanning.
...
Values to print or addresses that receive scanned values.
Return Value
Return Value
Description
Non-negative result
Success count, orientation, or status as defined by the function.
Negative / EOF
A conversion, input, output, or formatting failure.
C Examples
Example 1 — Print a wide string
This example uses swprintf() to print a wide string, then exposes the result so you can connect the call with its output.
Prepare a practical variation. The data is arranged specifically to combine values.
Match the format and arguments.swprintf(out, 32, L"%d", 42) uses a wide format string and correctly typed values or pointers.
Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
Interpret the output.score 9.5 confirms the behavior demonstrated by this example.
Frequently Asked Questions
swprintf() formats wide text into a buffer. It is part of the standard wide-character library.
Its parameters are ws, n, format, .... ws: A wide-string buffer, source string, or format string. n: The buffer capacity or maximum number of elements to process. format: A wide format string that controls printing or scanning. ...: Values to print or addresses that receive scanned values.
Non-negative result means success count, orientation, or status as defined by the function. Negative / EOF means a conversion, input, output, or formatting failure.
Include <wchar.h> for swprintf(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
The wide format string must match every argument exactly. A wrong specifier or missing destination address causes undefined behavior.