getwc() — 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 getwc(FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <wchar.h>

wint_t getwc_unlocked(FILE *stream);

General description

Obtains the next multibyte character from stdin, converts it to a wide character, and advances the associated file position indicator for stdin.

The getwc() function is equivalent to the fgetwc() function. Therefore, the argument should never be an expression with side effects.

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.

Using non-wide-character functions with getwc() results in undefined behavior. This happens because getwc() processes a whole multibyte character and does not expect to be “within” such a character. In addition, getwc() expects state information to be set already. Because functions like fgetc() and fputc() do not obey such rules, their results fail to meet the assumptions made by getwc().

getwc() has the same restriction as any read operation for a read immediately following a write or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must also be an intervening flush or reposition unless an EOF has been reached.

getwc_unlocked() is functionally equivalent to getwc() 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 stream or else the function returns WEOF.

If there is an error, getwc() sets the error indicator. If the EOF is encountered, it sets the EOF indicator. If an encoding error is encountered, it sets EILSEQ in errno.

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

CELEBG21
⁄* CELEBG21 *⁄                                   
#include <errno.h>                                                              
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
#include <wchar.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   FILE    *stream;                                                             
   wint_t   wc;                                                                 
                                                                                
   if ((stream = fopen("myfile.dat", "r")) == NULL) {                           
      printf("Unable to open file.");                                           
      exit(1);                                                                  
   }                                                                            
                                                                                
   errno = 0;                                                                   
   while ((wc = getwc(stream)) != WEOF)                                         
      printf("wc=0x%lx\n", wc);                                                 
                                                                                
   if (errno == EILSEQ) {                                                       
      printf("An invalid wide character was encountered.\n");                   
      exit(1);                                                                  
   }                                                                            
                                                                                
   fclose(stream);                                                              
}                                                                               

Related information