C++ putwc()

The putwc() function in C++ writes a wide character to the given output stream.

putwc() and fputwc() are similar in terms of functionality. However, a major difference between them is that putwc() can be implemented as a macro.

It is defined in <cwchar> header file.

putwc() prototype

wint_t putwc( wchar_t ch, FILE* stream );

The putwc() function takes a output file stream and a wide character ch as its arguments and writes wc to the file associated with stream.

putwc() Parameters

  • ch: The wide character to be written.
  • stream: The output file stream to write the wide character.

putwc() Return value

  • On success, the putwc() function returns ch.
  • On failure, it returns >WEOF. If an encoding error occurs, errno is set to EILSEQ.

Example: How putwc() function works?

#include <iostream>
#include <cwchar>
#include <cstdio>
#include <clocale>
using namespace std;

int main()
	setlocale(LC_ALL, "en_US.UTF-8");

	/* Devanagiri script */
	wchar_t str[] = L"देवनागरि";
	FILE *fp = fopen("file.txt","w");

	if (fp)
		for(int i=0; i<wcslen(str); i++)
		perror("File opening failed");

	return 0;

When you run the program, the following string will be written to file.txt:

Did you find this article helpful?