I am going through K&R
. I am bit confused with the following excerpt of stdio.h
.
typedef struct _iobuf {
int cnt; /* characters left */
char *ptr; /* next character position */
char *base; /* location of buffer */
int flag; /* mode of file access */
int fd; /* file descriptor */
} FILE;
extern FILE _iob[OPEN_MAX];
#define stdin (&_iob[0])
#define stdout (&_iob[1])
#define stderr (&_iob[2])
Here the FILE
is defined as a structure, and the stdin
, stdout
, stderr
are first three member of an array of type FILE
. So where is the assignment of (&_iob[0])
, (&_iob[1])
or (&_iob[2])
to standard input device
and standard output device
are written?
Here,
_iob[OPEN_MAX];
is declared asextern
variable asextern FILE _iob[OPEN_MAX];
. This means,_iob[OPEN_MAX];
is filled by some other code and there is an initial code which assigns(&_iob[0]), (&_iob[1]) or (&_iob[2]) to stdin, stdout and stderr