What is FILE in C?

Consider:

#include <stdio.h>
FILE * fopen(const char *restrict filename, const char *restrict mode);

fopen gives us a FILE, but what is a FILE?

First, FILE used to be a macro, which is why it’s capitalized.

Nowadays, it’s a typedef, defined in stdio.h. On macOS, in /usr/include/stdio.h:

typedef struct __sFILE {
        short   _file;          /* fileno, if Unix descriptor, else -1 */
        // ... more stuff ...
} FILE;

The rest of the definition has some buffers for doing getc, ungetc, etc.

Tagged #c, #programming.
👋 I'm Jim, a full-stack product engineer. Want to build an amazing product and a profitable business? Read more about me or Get in touch!

More by Jim

This page copyright James Fisher 2016. Content is not associated with my employer. Found an error? Edit this page.