C fwide() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
wchar.h

Definition

In C, fwide() checks or sets a stream's byte or wide orientation. It belongs to the wide-character tools declared in <wchar.h>.

Use wchar_t for wide characters and prefix wide literals with L, as in L"Hello". The examples below use simple C Programming Tutorial patterns and print wide strings with %ls.

Syntax

The following concrete call shows the argument shape before the formal declaration.

C
fwide(fp, 1);
CallWhat it does
fwide(fp, 1)Checks or sets a stream's byte or wide orientation in a typical call.

Include <wchar.h>. Add only the supporting headers actually used by your program.

Library declaration
int fwide(FILE *stream, int mode);

Parameters

ParameterDescription
streamThe FILE stream to read, write, inspect, or push back into.
modePositive requests wide orientation, negative requests byte orientation, and zero only queries.

Return Value

Return ValueDescription
PositiveThe stream is wide-oriented.
ZeroThe stream is still unoriented.
NegativeThe stream is byte-oriented. This is not an I/O failure.

C Examples

Example 1 — Request wide orientation

This example uses fwide() to request wide orientation, then exposes the result so you can connect the call with its output.

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

int main(void) {
    FILE *fp = tmpfile();
    int orientation = fwide(fp, 1);
    wprintf(L"wide request = %ls\n", orientation > 0 ? L"yes" : L"not set");
    fclose(fp);
    return 0;
}

How It Works

  1. Set up this case. The program prepares the exact input needed to request wide orientation.
  2. Use the stream correctly. fwide(fp, 1) performs the wide-oriented stream operation.
  3. Read the normal result. The displayed output shows the direct result of the call for this input.
  4. Interpret the output. wide request = yes confirms the behavior demonstrated by this example.

Example 2 — Query orientation

This example uses fwide() to query orientation, then exposes the result so you can connect the call with its output.

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

int main(void) {
    FILE *fp = tmpfile();
    int orientation = fwide(fp, 1);
    wprintf(L"wide request = %ls\n", orientation > 0 ? L"yes" : L"not set");
    fclose(fp);
    return 0;
}

How It Works

  1. Choose the edge case. This example changes the input so you can see how fwide() behaves for query orientation.
  2. Use the stream correctly. fwide(fp, 1) performs the wide-oriented stream operation.
  3. Inspect the status or pointer. The program checks what the function returned before relying on output data.
  4. Interpret the output. wide request = yes confirms the behavior demonstrated by this example.

Example 3 — Orient before I/O

This example uses fwide() to orient before i/o, then exposes the result so you can connect the call with its output.

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

int main(void) {
    FILE *fp = tmpfile();
    int orientation = fwide(fp, 1);
    wprintf(L"wide request = %ls\n", orientation > 0 ? L"yes" : L"not set");
    fclose(fp);
    return 0;
}

How It Works

  1. Prepare a practical variation. The data is arranged specifically to orient before i/o.
  2. Use the stream correctly. fwide(fp, 1) performs the wide-oriented stream operation.
  3. Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
  4. Interpret the output. wide request = yes confirms the behavior demonstrated by this example.

Frequently Asked Questions

fwide() checks or sets a stream's byte or wide orientation. It is part of the standard wide-character library.
Its parameters are stream, mode. stream: The FILE stream to read, write, inspect, or push back into. mode: Positive requests wide orientation, negative requests byte orientation, and zero only queries.
Positive means the stream is wide-oriented. Zero means the stream is still unoriented. Negative means the stream is byte-oriented. This is not an I/O failure.
Include <wchar.h> for fwide(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
Do not mix byte I/O and wide I/O after a stream becomes oriented. Check WEOF or the documented status before using data.

Next: C fwprintf() Function

Continue learning C wide-character functions.

Next tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful