What is fwprintf() in C?

fwprintf() writes the wide-character C-string pointed by the format pointer to the specified stream. If the format string contains any format specifiers (%s, %d etc …), the the additional arguments corresponding to these specifiers are passed after the format string.

Wide-characters are a typedef of short int and are 16-bits (2 bytes) long which allows them to encode Unicode characters.

Prototype

int fwprintf (FILE* stream, const wchar_t* format, ...);

Parameters

  • FILE* stream: A pointer of type FILE* that points to the stream where the format string needs to be written to.

  • const chat_t* format: A pointer to the wide-character array which is written to the output stream specified by stream.

  • ...(additional arguments): If the format string contains any format specifiers, then the additional arguments that replace the corresponding format specifiers go here.

Return value

  • Upon success, fwprintf() returns the number of characters printed. If the function fails, it returns a negative value and raises the ferror flag.

Example

#include <stdio.h>
#include <wchar.h>
int main() {
wchar_t output[80] = L"This is a wide-character format %s\n";
//create an output file and write the contents to it
FILE* outputFile;
outputFile = fopen("Temp.txt", "w");
fwprintf(outputFile, output, "string");
//write to standard output
fwprintf(stdout, output, "string");
return 0;
}

In the code above, we create an output array of wchar_t, which represents wide characters in C. The L in front of the string specifies that the string is a wide string.

L"THIS IS A WIDE STRING";

The FILE* outputFile is a file pointer to the output stream which will be our "Temp.txt" file, in which the contents of output will be written.

fwprintf(outputFile, output, "string")

The output array also contains string format specifier (%s) that allows us to pass in an additional string argument after the output array that will replace the format specifier.

We can also pass in the stdout as the output stream, which will cause the contents to be written to the standard output or the terminal.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved