Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

declaration - where is stdin defined in c standard library?

I found this line in stdio.h :

extern struct _IO_FILE *stdin;

Based on this 'extern' keyword, i assume this is just a declaration. I wonder where is stdin defined and initialized?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's defined in the source code of your C library. You typically only need the headers for compilation, but you can find the source code for many open-source standard libraries (like glibc).

In glibc, it's defined in libio/stdio.c as like this:

_IO_FILE *stdin = (FILE *) &_IO_2_1_stdin_;

Which is in turn defined using a macro in libio/stdfiles.c like this:

DEF_STDFILE(_IO_2_1_stdin_, 0, 0, _IO_NO_WRITES);

The definition of the DEF_STDFILE macro varies depending on a few things, but it more or less sets up an appropriate FILE struct using the file descriptor 0 (which is standard input on Unix).

The definition may (and of course does) vary depending on your C library, and certainly by platform. If you want, you can continue the goose chase around the various parts of your standard library's I/O component.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...