putwc() — Output a wide character

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3
Language Environment

both  

Format

Non-XPG4:
#include <stdio.h>
#include <wchar.h>

wint_t putwc(wchar_t wc, FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <wchar.h>

wint_t putwc_unlocked(wchar_t wc, FILE *stream);
XPG4:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <wchar.h>

wint_t putwc(wint_t wc, FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <wchar.h>

wint_t putwc_unlocked(wchar_t wc, FILE *stream);
XPG4 and MSE:
#define _XOPEN_SOURCE
#define _MSE_PROTOS
#include <stdio.h>
#include <wchar.h>

wint_t putwc(wchar_t wc, FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <wchar.h>

wint_t putwc_unlocked(wchar_t wc, FILE *stream);

General description

The putwc() function is equivalent to the fputwc() function, except that if it is implemented as a macro, it may evaluate stream more than once. 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 use a non-wide-oriented function with putwc(), undefined results can occur.

putwc() has the same restriction as any write 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.

putwc_unlocked() is functionally equivalent to putwc() 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.

Special behavior for XPG4: If you define any feature test macro specifying XPG4 behavior before the statement in your program source file to include the wchar header, then the compiler assumes that your program is using the XPG4 variety of the putwc() function, unless you also define the _MSE_PROTOS feature test macro. Please see Table 1 for a list of XPG4 and other feature test macros.

The prototype for the XPG4 variety of the putwc() function is:
wint_t putwc(wint_t wc, FILE *stream);

The difference between this variety and the MSE variety of the putwc() function is that the first parameter has type wint_t rather than type wchar_t.

Returned value

If successful, putwc() returns the wide character written.

If a write error occurs, the error indicator for the stream is set and WEOF is returned. If an encoding error occurs when converting from a wide character to a multibyte character, the value of the macro EILSEQ is stored in errno and WEOF is returned.

Example

CELEBP56
⁄* CELEBP56 *⁄                                   
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
#include <wchar.h>                                                              
#include <errno.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   FILE    *stream;                                                             
   wchar_t *wcs = L"This test string should not cause a WEOF condition";        
   int     i;                                                                   
   int     rc;                                                                  
                                                                                
   if ((stream = fopen("myfile.dat", "w")) == NULL) {                           
      printf("Unable to open file\n");                                          
      exit(1);                                                                  
   }                                                                            
                                                                                
   for (i=0; wcs[i] != L'\0'; i++) {                                            
      errno = 0;                                                                
      if ((rc = putwc(wcs[i], stream)) == WEOF) {                               
         printf("Unable to putwc() the wide character.\n");                     
         printf("wcs[%d] = 0x%X\n", i, wcs[i]);                                 
         if (errno == EILSEQ)                                                   
            printf("An invalid wide character was encountered.\n");             
         exit(1);                                                               
      }                                                                         
   }                                                                            
                                                                                
   fclose(stream);                                                              
}                                                                               

Related information