Home C wchar.h Functions getwc() C getwc() Function Definition In C, getwc() reads one wide character from a stream. 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.
Call What it does getwc(fp)Reads one wide character from a stream in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
Library declaration wint_t getwc(FILE *stream);Parameters Parameter Description streamThe FILE stream to read, write, inspect, or push back into.
Return Value Return Value Description Wide character The character read, written, pushed back, or converted. WEOF End of file or failure.
C Examples 1. Basic stream operation 2. Check the returned status 3. Use wide-oriented I/O Example 1 — Basic stream operation This example uses getwc() to basic stream operation, then exposes the result so you can connect the call with its output.
#include <stdio.h>
#include <wchar.h>
int main(void) {
FILE *fp = tmpfile();
fputws(L"A", fp); rewind(fp);
wprintf(L"%lc\n", getwc(fp)); fclose(fp);
return 0;
} How It Works Set up this case. The program prepares the exact input needed to basic stream operation.Use the stream correctly. getwc(fp) performs the wide-oriented stream operation.Read the normal result. The displayed output shows the direct result of the call for this input.Interpret the output. A confirms the behavior demonstrated by this example.Example 2 — Check the returned status This example uses getwc() to check the returned status, then exposes the result so you can connect the call with its output.
#include <stdio.h>
#include <wchar.h>
int main(void) {
FILE *fp = tmpfile();
fputws(L"A", fp); rewind(fp);
wprintf(L"%lc\n", getwc(fp)); fclose(fp);
return 0;
} How It Works Choose the edge case. This example changes the input so you can see how getwc() behaves for check the returned status.Use the stream correctly. getwc(fp) performs the wide-oriented stream operation.Inspect the status or pointer. The program checks what the function returned before relying on output data.Interpret the output. A confirms the behavior demonstrated by this example.Example 3 — Use wide-oriented I/O This example uses getwc() to use wide-oriented i/o, then exposes the result so you can connect the call with its output.
#include <stdio.h>
#include <wchar.h>
int main(void) {
FILE *fp = tmpfile();
fputws(L"A", fp); rewind(fp);
wprintf(L"%lc\n", getwc(fp)); fclose(fp);
return 0;
} How It Works Prepare a practical variation. The data is arranged specifically to use wide-oriented i/o.Use the stream correctly. getwc(fp) performs the wide-oriented stream operation.Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.Interpret the output. A confirms the behavior demonstrated by this example.Frequently Asked Questions What does getwc() do in C? getwc() reads one wide character from a stream. It is part of the standard wide-character library.
What parameters does getwc() take? Its parameters are stream. stream: The FILE stream to read, write, inspect, or push back into.
What does getwc() return? Wide character means the character read, written, pushed back, or converted. WEOF means end of file or failure.
Which header declares getwc()? Include <wchar.h> for getwc(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
What is a common getwc() mistake? 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 getwchar() Function Continue learning C wide-character functions.
Next tutorial → About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion