getwchar() — Get Wide Character from stdin

Format

     #include <wchar.h>
     wint_t getwchar(void);

Language Level: ANSI

Threadsafe: Yes.

Locale Sensitive: The behavior of this function might be affected by the LC_CTYPE category of the current locale. It might also be affected by the LC_UNI_CTYPE category of the current locale if LOCALETYPE(*LOCALEUCS2) or LOCALETYPE(*LOCALEUTF) is specified on the compilation command. This function is not available when LOCALETYPE(*CLD) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.

Integrated File System Interface: This function is not available when SYSIFCOPT(*NOIFSIO) is specified on the compilation command.

Wide Character Function: See Wide Characters for more information.

Description

The getwchar() function reads the next multibyte character from stdin, converts it to a wide character, and advances the associated file position indicator for stdin. A call to the getwchar() function is equivalent to a call to getwc(stdin).

If the current locale is changed between subsequent read operations on the same stream, undefined results can occur. Using non-wide-character functions with the getwchar() function on stdin results in undefined behavior.

Return Value

The getwchar() function returns the next wide character from stdin or WEOF. If the getwchar() function encounters EOF, it sets the EOF indicator for the stream and returns WEOF. If a read error occurs, the error indicator for the stream is set, and the getwchar() function returns WEOF. If an encoding error occurs during the conversion of the multibyte character to a wide character, the getwchar() function sets errno to EILSEQ and returns WEOF.

Use the ferror() or feof() functions to determine whether an error or an EOF condition occurred. 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.

For information about errno values for getwchar(), see fgetwc() — Read Wide Character from Stream.

Example that uses getwchar()

This example uses the getwchar() to read wide characters from the keyboard, then prints the wide characters.

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
 
int main(void)
{
   wint_t  wc;
 
   errno = 0;
   while (WEOF != (wc = getwchar()))
      printf("wc = %lc\n", wc);
 
   if (EILSEQ == errno) {
      printf("An invalid wide character was encountered.\n");
      exit(1);
   }
   return 0;
 
   /***************************************************************
      Assuming you enter: abcde
 
      The output should be:
 
      wc = a
      wc = b
      wc = c
      wc = d
      wc = e
   ***************************************************************/
}

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]