getwchar() — Get a wide character

Standards

Standards / Extensions C or C++ Dependencies
ISO C Amendment
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3 Language Environment®
both  

Format

#include <wchar.h>

wint_t getwchar(void)

#define _OPEN_SYS_UNLOCKED_EXT 1;
#include <wchar.h>

wint_t getwchar_unlocked(void)

General description

The getwchar() function is equivalent to getwc() with the argument stdin.

The behavior of this wide-character function is affected by the LC_CTYPE category of the current locale. If you change the category, undefined results can occur.

getwchar_unlocked() is functionally equivalent to getwchar() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Returned value

Returns the next wide character from the input stream pointed to by stdin or else the function returns WEOF. If the stream is at EOF, the EOF indicator for the stream is set and fgetwc() returns WEOF. If a read error occurs, the error indicator for the stream is set and fgetwc() returns WEOF. If an encoding error occurs, the value of the macro EILSEQ is stored in errno and WEOF is returned.

Use ferror() or feof() to determine whether an error or an EOF condition occurred. Note that EOF is only reached when an attempt is made to read past the last byte of data. Reading up to and including the last byte of data does not turn on the EOF indicator.

Example

CELEBG22
/* CELEBG22 */                                   
#include <errno.h>                                                              
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
#include <wchar.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   wint_t   wc;                                                                 
                                                                                
   errno = 0;                                                                   
   while ((wc = getwchar()) != WEOF)                                            
      printf("wc=0x%X\n", wc);                                                  
                                                                                
   if (errno == EILSEQ) {                                                       
      printf("An invalid wide character was encountered.\n");                   
      exit(1);                                                                  
   }                                                                            
}                                                                               

Related information