### putwc -- Write Wide Character

Format

>     #include <stdio.h>
>     #include <wchar.h>
>     wint_t putwc(wint_t wc, FILE *stream);

Language Level: ANSI 93, XPG4
putwc converts the wide character wc to a multibyte character, and writes it to the stream at the current position. It also advances the file position indicator for the stream appropriately.

putwc function is equivalent to fputwc except that, if it is implemented as a macro, putwc can evaluate stream more than once. Therefore, the stream argument to putwc should not be an expression with side effects.

The behavior of putwc is affected by the LC_CTYPE category of the current locale. Using a non-wide-character function with putwc on the same stream results in undefined behavior.

After calling putwc, flush the buffer or reposition the stream pointer before calling a write function for the stream, unless EOF has been reached. After a write operation on the stream, flush the buffer or reposition the stream pointer before calling putwc.

Return Value
putwc returns the wide character written. If a write error occurs, putwc sets the error indicator for the stream and returns WEOF. If an encoding error occurs when a wide character is converted to a multibyte character, putwc sets errno to EILSEQ and returns WEOF.

Example
The following example uses putwc to convert the wide characters in wcs to multibyte characters and write them to the file putwc.out.

    #include <stdio.h>
    #include <stdlib.h>
    #include <wchar.h>
    #include <errno.h>

    int main(void)
    {
       FILE    *stream;
       wchar_t *wcs = L"A character string.";
       int     i;

       if (NULL == (stream = fopen("fputwc.out", "w"))) {
          printf("Unable to open: \"fputwc.out\".\n");
          exit(1);
       }

       for (i = 0; wcs[i] != L'\0'; i++) {
          errno = 0;
          if (WEOF == fputwc(wcs[i], stream)) {
             printf("Unable to fputwc() the wide character.\n"
                    "wcs[%d] = 0x%lx\n", i, wcs[i]);
             if (EILSEQ == errno)
                printf("An invalid wide character was encountered.\n");
             exit(1);
          }
       }
       fclose(stream);
       return 0;

       /***************************************************************
          The output file fputwc.out should contain :

          A character string.
       ***************************************************************/
    }

fputc -- Write Character
_fputchar -- Write Character
fputwc -- Write Wide Character
getwc -- Read Wide Character from Stream
putc - putchar -- Write a Character
_putch -- Write Character to Screen
putwchar -- Write Wide Character to stdout
<stdio.h>
<wchar.h>