C++ Standard Library C++ STL Library

C++ <cwchar> - getwchar() Function



The C++ <cwchar> getwchar() function returns the next wide character from the input stream stdin. It is equivalent to calling getwc() function with stdin as second argument.

If the sequence of bytes read cannot be interpreted as a valid wide character, the function returns WEOF and sets errno to EILSEQ.

If a reading error occurs, the function returns WEOF and sets the error indicator for the stream ferror().

If the stream is at end-of-file when the function is called, it returns WEOF and sets end-of-file indicator for the stream feof().

Syntax

wint_t getwchar();

Parameters

No parameter is required.

Return Value

  • On success, the character read is returned.
  • If the failure has been caused due to end-of-file condition, returns WEOF and sets the end-of-file indicator for the stream feof().
  • If a read error occurs, the function returns WEOF and sets the error indicator for the stream ferror().
  • If the sequence of bytes read cannot be interpreted as a valid wide character, the function returns WEOF and sets errno to EILSEQ.

Example:

The example below shows the usage of getwchar() function.

#include <cwchar>
 
int main (){

  wint_t wc;

  //prints characters till the point
  //it encounters the first '@' character
  wprintf(L"Enter text (Include '@' in the sentence to exit):");
  do {
    wc = getwchar();
    putwchar(wc);
  } while (wc != L'@');
  
  return 0;
}

If the following inputs are entered:

John
25
London
John@example.com

The output will be:

John
25
London
John@

❮ C++ <cwchar> Library

5