C wscanf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
wchar.h

Definition

In C, wscanf() reads formatted wide input from standard input. 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
wscanf(L"%d", &number);
CallWhat it does
wscanf(L"%d", &number)Reads formatted wide input from standard input in a typical call.

Include <wchar.h>. Add only the supporting headers actually used by your program.

Library declaration
int wscanf(const wchar_t *restrict format, ...);

Parameters

ParameterDescription
formatA wide format string that controls printing or scanning.
...Values to print or addresses that receive scanned values.

Return Value

Return ValueDescription
Non-negative resultSuccess count, orientation, or status as defined by the function.
Negative / EOFA conversion, input, output, or formatting failure.

C Examples

Example 1 — Parse two fields

This example uses wscanf() 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;
    wprintf(L"Enter an integer and letter: ");
    int assigned = wscanf(L"%d %lc", &number, &letter);
    wprintf(L"%d fields: %d %lc\n", assigned, number, letter);
    return 0;
}

How It Works

  1. Set up this case. The program prepares the exact input needed to parse two fields.
  2. Match the format and arguments. wscanf(L"%d", &number) uses a wide format string and correctly typed values or pointers.
  3. Read the normal result. The displayed output shows the direct result of the call for this input.
  4. Interpret the output. Enter an integer and letter: 42 C / 2 fields: 42 C confirms the behavior demonstrated by this example.

Example 2 — Match destination pointer types

This example uses wscanf() 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;
    wprintf(L"Enter an integer and letter: ");
    int assigned = wscanf(L"%d %lc", &number, &letter);
    wprintf(L"%d fields: %d %lc\n", assigned, number, letter);
    return 0;
}

How It Works

  1. Choose the edge case. This example changes the input so you can see how wscanf() behaves for match destination pointer types.
  2. Match the format and arguments. wscanf(L"%d", &number) uses a wide format string and correctly typed values or pointers.
  3. Inspect the status or pointer. The program checks what the function returned before relying on output data.
  4. Interpret the output. Enter an integer and letter: 42 C / 2 fields: 42 C confirms the behavior demonstrated by this example.

Example 3 — Check the assignment count

This example uses wscanf() 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;
    wprintf(L"Enter an integer and letter: ");
    int assigned = wscanf(L"%d %lc", &number, &letter);
    wprintf(L"%d fields: %d %lc\n", assigned, number, letter);
    return 0;
}

How It Works

  1. Prepare a practical variation. The data is arranged specifically to check the assignment count.
  2. Match the format and arguments. wscanf(L"%d", &number) uses a wide format string and correctly typed values or pointers.
  3. Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
  4. Interpret the output. Enter an integer and letter: 42 C / 2 fields: 42 C confirms the behavior demonstrated by this example.

Frequently Asked Questions

wscanf() reads formatted wide input from standard input. It is part of the standard wide-character library.
Its parameters are format, .... 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 wscanf(). 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.

Next: C wchar.h Functions

Continue learning C wide-character functions.

Next tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful