In C, swscanf() parses formatted data from a wide string. 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
swscanf(L"42", L"%d", &number);
Call
What it does
swscanf(L"42", L"%d", &number)
Parses formatted data from a wide string in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
Library declaration
int swscanf(const wchar_t *restrict ws, const wchar_t *restrict format, ...);
Parameters
Parameter
Description
ws
A wide-string buffer, source string, or format string.
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 — Parse two fields
This example uses swscanf() to parse two fields, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
int number; wchar_t letter;
int assigned = swscanf(L"42 C", L"%d %lc", &number, &letter);
wprintf(L"%d fields: %d %lc\n", assigned, number, letter);
return 0;
}
Output
2 fields: 42 C
How It Works
Set up this case. The program prepares the exact input needed to parse two fields.
Match the format and arguments.swscanf(L"42", L"%d", &number) uses a wide format string and correctly typed values or pointers.
Read the normal result. The displayed output shows the direct result of the call for this input.
Interpret the output.2 fields: 42 C confirms the behavior demonstrated by this example.
Example 2 — Match destination pointer types
This example uses swscanf() to match destination pointer types, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
int number; wchar_t letter;
int assigned = swscanf(L"42 C", L"%d %lc", &number, &letter);
wprintf(L"%d fields: %d %lc\n", assigned, number, letter);
return 0;
}
Output
2 fields: 42 C
How It Works
Choose the edge case. This example changes the input so you can see how swscanf() behaves for match destination pointer types.
Match the format and arguments.swscanf(L"42", L"%d", &number) uses a wide format string and correctly typed values or pointers.
Inspect the status or pointer. The program checks what the function returned before relying on output data.
Interpret the output.2 fields: 42 C confirms the behavior demonstrated by this example.
Example 3 — Check the assignment count
This example uses swscanf() to check the assignment count, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
int number; wchar_t letter;
int assigned = swscanf(L"42 C", L"%d %lc", &number, &letter);
wprintf(L"%d fields: %d %lc\n", assigned, number, letter);
return 0;
}
Output
2 fields: 42 C
How It Works
Prepare a practical variation. The data is arranged specifically to check the assignment count.
Match the format and arguments.swscanf(L"42", L"%d", &number) 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.2 fields: 42 C confirms the behavior demonstrated by this example.
Frequently Asked Questions
swscanf() parses formatted data from a wide string. It is part of the standard wide-character library.
Its parameters are ws, format, .... ws: A wide-string buffer, source string, or format string. 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 swscanf(). 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.