wprintf() and wscanf in C Library - GeeksforGeeks

19 Dec, 2017
If you are dealing with the wide characters then printf() and scanf() function cannot be used. There are different functions for input and output C wide string.
wprintf() : The wprintf() function writes the wide string pointed to by format to stdout. The wide string format may contain format specifiers starting with % which are replaced by the values of variables that are passed to the wprintf() function as additional arguments.Syntax :int wprintf (const wchar_t* format, ...);Parameters :format : A pointer to a null terminated wide string that is written to stdout. It consists of wide characters along with optional format specifiers starting with %. The format specifiers are replaced by the values of respective variables that follows format.... : Other additional arguments specifying the data to be printed. They occur in a sequence according to the format specifier.There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.Return Value :If successful, the wprintf() function returns number of characters written. On failure it returns a negative value. If successful, the wprintf() function returns number of characters written.On failure it returns a negative value.// C Program to show the wprintf () function.#include <stdio.h>#include <wchar.h> // Header file containing wprintf function// Driver codeint main(){    wint_t x = 5;    wchar_t name[] = L"GEEKS";    wprintf(L"x = %d \n", x);    wprintf(L"HELLO %ls \n", name);    return 0;}Output:x = 5 
HELLO GEEKS
wscanf() : The wscanf() function reads the data from stdin and stores the values into the respective variables. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.Syntax :int wscanf (const wchar_t* format, ...);Parameters :format : Pointer to a null-terminated character string that specifies how to read the input. It consists of format specifiers starting with %. Notice though, that all format specifiers have the same meaning as in scanf; therefore, %lc shall be used to read a wide character (and not %c), as well as %ls shall be used for wide strings (and not %s).... : Other additional arguments for receiving data. They occur in a sequence according to the format specifier.There should be at least as many of these arguments as the number of values stored by the format specifiers. Additional arguments are ignored by the function.Return Value : The wscanf() function returns the number of receiving arguments successfully assigned. We can count and match the expected number of items or be less due to a matching failure, a reading error is occured, or the reach of the end-of-file causes an error.If failure occurs before the first receiving argument was assigned, EOF is returned.// Program to show the wprintf () function.#include <stdio.h>#include <wchar.h> // Header file containing wscanf() functionint main(){    wchar_t str[80];    int i;      wscanf(L"%ls", str);    wscanf(L"%d", &i);    wprintf(L"I am a %ls of CSE in %d year.\n", str, i);    return 0;}Input:GEEK
2
Output:I am a GEEK of CSE in 2 year.

wprintf() : The wprintf() function writes the wide string pointed to by format to stdout. The wide string format may contain format specifiers starting with % which are replaced by the values of variables that are passed to the wprintf() function as additional arguments.Syntax :int wprintf (const wchar_t* format, ...);Parameters :format : A pointer to a null terminated wide string that is written to stdout. It consists of wide characters along with optional format specifiers starting with %. The format specifiers are replaced by the values of respective variables that follows format.... : Other additional arguments specifying the data to be printed. They occur in a sequence according to the format specifier.There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.Return Value :If successful, the wprintf() function returns number of characters written. On failure it returns a negative value. If successful, the wprintf() function returns number of characters written.On failure it returns a negative value.// C Program to show the wprintf () function.#include <stdio.h>#include <wchar.h> // Header file containing wprintf function// Driver codeint main(){    wint_t x = 5;    wchar_t name[] = L"GEEKS";    wprintf(L"x = %d \n", x);    wprintf(L"HELLO %ls \n", name);    return 0;}Output:x = 5 
HELLO GEEKS

Syntax :
int wprintf (const wchar_t* format, ...);
Parameters :
format : A pointer to a null terminated wide string that is written to stdout. It consists of wide characters along with optional format specifiers starting with %. The format specifiers are replaced by the values of respective variables that follows format.
... : Other additional arguments specifying the data to be printed. They occur in a sequence according to the format specifier.There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
Return Value :If successful, the wprintf() function returns number of characters written. On failure it returns a negative value. If successful, the wprintf() function returns number of characters written.On failure it returns a negative value.
// C Program to show the wprintf () function.#include <stdio.h>#include <wchar.h> // Header file containing wprintf function// Driver codeint main(){    wint_t x = 5;    wchar_t name[] = L"GEEKS";    wprintf(L"x = %d \n", x);    wprintf(L"HELLO %ls \n", name);    return 0;}
Output:
x = 5 
HELLO GEEKS

wscanf() : The wscanf() function reads the data from stdin and stores the values into the respective variables. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.Syntax :int wscanf (const wchar_t* format, ...);Parameters :format : Pointer to a null-terminated character string that specifies how to read the input. It consists of format specifiers starting with %. Notice though, that all format specifiers have the same meaning as in scanf; therefore, %lc shall be used to read a wide character (and not %c), as well as %ls shall be used for wide strings (and not %s).... : Other additional arguments for receiving data. They occur in a sequence according to the format specifier.There should be at least as many of these arguments as the number of values stored by the format specifiers. Additional arguments are ignored by the function.Return Value : The wscanf() function returns the number of receiving arguments successfully assigned. We can count and match the expected number of items or be less due to a matching failure, a reading error is occured, or the reach of the end-of-file causes an error.If failure occurs before the first receiving argument was assigned, EOF is returned.// Program to show the wprintf () function.#include <stdio.h>#include <wchar.h> // Header file containing wscanf() functionint main(){    wchar_t str[80];    int i;      wscanf(L"%ls", str);    wscanf(L"%d", &i);    wprintf(L"I am a %ls of CSE in %d year.\n", str, i);    return 0;}Input:GEEK
2
Output:I am a GEEK of CSE in 2 year.

Syntax :
int wscanf (const wchar_t* format, ...);
Parameters :
format : Pointer to a null-terminated character string that specifies how to read the input. It consists of format specifiers starting with %. Notice though, that all format specifiers have the same meaning as in scanf; therefore, %lc shall be used to read a wide character (and not %c), as well as %ls shall be used for wide strings (and not %s).
... : Other additional arguments for receiving data. They occur in a sequence according to the format specifier.There should be at least as many of these arguments as the number of values stored by the format specifiers. Additional arguments are ignored by the function.
Return Value : The wscanf() function returns the number of receiving arguments successfully assigned. We can count and match the expected number of items or be less due to a matching failure, a reading error is occured, or the reach of the end-of-file causes an error.If failure occurs before the first receiving argument was assigned, EOF is returned.
// Program to show the wprintf () function.#include <stdio.h>#include <wchar.h> // Header file containing wscanf() functionint main(){    wchar_t str[80];    int i;      wscanf(L"%ls", str);    wscanf(L"%d", &i);    wprintf(L"I am a %ls of CSE in %d year.\n", str, i);    return 0;}
Input:
GEEK
2

Output:
I am a GEEK of CSE in 2 year.

C-Library
C-String
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
TCP Server-Client implementation in C
Exception Handling in C++
Multithreading in C
Arrow operator -> in C/C++ with Examples
'this' pointer in C++
How to split a string in C/C++, Python and Java?
Smart Pointers in C++ and How to Use Them
UDP Server-Client implementation in C
How to dynamically allocate a 2D array in C?
Unordered Sets in C++ Standard Template Library