wprintf() and wscanf in C Library

In C programming, wprintf() and wscanf() are wide character equivalents of printf() and scanf() functions. These functions handle wide characters and wide strings, making them essential for internationalization and Unicode text processing. Both functions are declared in the wchar.h header file.

wprintf() Syntax

int wprintf(const wchar_t* format, ...);

Parameters

  • format − A pointer to a null-terminated wide string containing format specifiers starting with %
  • ... − Additional arguments corresponding to the format specifiers

Return Value

Returns the number of wide characters printed on success, or a negative value on failure.

Example: Using wprintf()

#include <stdio.h>
#include <wchar.h>

int main() {
    wint_t my_int = 10;
    wchar_t string[] = L"Hello World";
    
    wprintf(L"The my_int is: %d <br>", my_int);
    wprintf(L"The string is: %ls <br>", string);
    
    return 0;
}
The my_int is: 10
The string is: Hello World

wscanf() Syntax

int wscanf(const wchar_t* format, ...);

Parameters

  • format − A pointer to a null-terminated wide string containing format specifiers
  • ... − Pointers to variables where the scanned data will be stored

Return Value

Returns the number of input fields successfully scanned and stored.

Example: Using wscanf()

Note: This example demonstrates wscanf() usage but cannot be executed online as it requires interactive input.
#include <stdio.h>
#include <wchar.h>

int main() {
    wint_t my_int;
    wchar_t name[50];
    
    wprintf(L"Enter a number: ");
    wscanf(L"%d", &my_int);
    
    wprintf(L"Enter your name: ");
    wscanf(L"%ls", name);
    
    wprintf(L"Number: %d, Name: %ls<br>", my_int, name);
    
    return 0;
}

Key Differences from printf() and scanf()

Function Character Type String Literal Prefix Format Specifier
printf() char "string" %s
wprintf() wchar_t L"string" %ls

Common Format Specifiers

  • %d − Wide character representation of integers
  • %ls − Wide character strings
  • %lc − Single wide characters
  • %f − Floating-point numbers

Conclusion

The wprintf() and wscanf() functions provide wide character support for formatted output and input operations. They are essential when working with Unicode text or international applications that require multi-byte character handling.

Updated on: 2026-03-15T10:41:37+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements