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.

Similar posts

More by Jim

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

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