Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use fwprintf_s() in C?

Kainat Asif

fwprintf_s() prints a formatted string to an output stream and validates the arguments at runtime before printing them. The declaration for fwprintf_s() is shown below:

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

Note: The fwprintf_s function is not supported by some compilers. If a compiler supports fwprintf_s, the value of STDC_WANT_LIB_EXT1 is set to 1.


  • stream: Pointer to the output stream where output is to be printed.
  • format: White character string to be written to the output stream.
  • ...(additional arguments): If format consists of format specifiers (e.g., %s, %d, …), the additional arguments consist of the strings to be printed.

Return value

The fwprintf_s() function returns the number of characters written to the output stream if the data is written successfully. A negative value is returned if there is an error when writing to the output stream.

Note: The return value of fwprintf_s() does not count terminating null characters.


Consider the code snippet below, which demonstrates the use of fwprintf_s():

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

int main() {
    wchar_t output[80] = L"This is a wide-character format %s\n";  
    FILE* fPointer = fopen("fwprintfExample.txt", "w");

    fwprintf_s(fPointer, L"fwprintf() writing to %s\n", "file");
    fwprintf_s(stdout, L"fwprintf() writing to %s\n", "stdout");
    return 0;


fwprintf() writing to stdout


The fopen() function is used in line 7 to open the file for writing. The fwprintf_s()() function is used in line 9 to write to the file.

The contents of fwprintfExample.txt after executing the above code will be as follows:

# fwprintfExample.txt
fwprintf() writing to file

Another example of fwprintf_s() is given in line 11, which writes to stdout.

Note: The function above is not supported by the GCC compiler, hence you will get an implicit declaration of function fwprintf_s error. Use the following variant to get the job done: int fwprintf( FILE *stream, const wchar_t *format, ...);.




Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring