Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is getwchar in C?

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The getwchar function

The getwchar function is equivalent to the getch function in C. The only difference between the two is that getwchar works on wide characterssame as character data type, but take either 2 or 4 bytes space, whereas character data type takes 1 byte space, whereas getch works on normal characters.

The structure of the getwchar function is as follows:

wint_t getwchar( void );

The function getwchar reads a wide character from the standard input. If the sequence of bytes cannot be interpreted as a wide character, then the function returns the value WEOF.

Note: The reason that getwchar returns wint_t rather than wchar_t is to accommodate the special value WEOF.

To use the getwchar function, you need to include the wchar.h standard library.

Example usage of the getwchar function

The following example reads a character from the standard input and outputs it onto the console, unless x is the input:

Note: You need to provide an input in the STDIN of the following code widget (e.g., abcx).

#include<stdio.h>
#include<wchar.h>
int main() {
wint_t wchar_in = L'a';
do {
printf("\nInput any character (or 'x' to terminate): \n");
wchar_in = getwchar();
putwchar(wchar_in);
} while(wchar_in != L'x');
return 0;
}

Enter the input below

RELATED TAGS

c

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring