I\'ve started learning some C as a hobby and have blindly used FILE as a declaration for file pointers for quite some time, and I\'ve been wondering. Is this a keyword or sp
FILE
is an identifier used as a typedef name, usually for a struct
.
The stdio library usually has something like
typedef struct {
...
} FILE;
somewhere. All stdio functions dealing with FILE pointers know the contens of ...
and can access the structure members. The C programmers must use functions like fopen
, feof
, ferror
, ungetc
etc to create and operate on FILE structures. Such types are called opaque (i.e. you can´t peek inside them but must use accessor functions).
Why is it defined as a pointer?
It isn't. It's a struct to which your code declares a pointer. Note the asterisk in your
FILE* fp;
which is another example of why the asterisk should go with the variable identifier, not the type name:
FILE *fp;