wprintf() and wscanf in C Library


Here we will see the wprintf() and wscanf() functions in C. These are the printf() and scanf() functions for wide characters. These functions are present in the wchar.h

The wprintf() function is used to print the wide character to the standard output. The wide string format may contain the format specifiers which is starting with % sign, these are replaced by the values of variables which are passed to the wprintf().

The syntax is like below −

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

This function takes the format. This format is a pointer to a null terminated wide string, that will be written in the console. It will hold wide characters and some format specifiers starting with %. Then the (…) is indicating the additional arguments. These are the data that will be printed, they occur in a sequence according to the format specifiers.

This function returns the number of printed characters. On failure, this may return negative values.

Example

#include <stdio.h>
#include <wchar.h>
main() {
   wint_t my_int = 10;
   wchar_t string[] = L"Hello World";
   wprintf(L"The my_int is: %d 
", my_int);    wprintf(L"The string is: %ls
", string); }

Output

The my_int is: 10
The string is: Hello World

The wscanf() function is used to take the data from console, and store them into proper variable. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier inside the format string.

Example

#include <stdio.h>
#include <wchar.h>
main() {
   wint_t my_int = 10;
   wprintf(L"Enter a number: ");
   wscanf(L"%d", &my_int);
   wprintf(L"The given integer is: %d 
", my_int); }

Output

Enter a number: 40
The given integer is: 40

Updated on: 30-Jul-2019

957 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements