问题描述
我在stdio.h中找到了这一行:
I found this line in stdio.h :
extern struct _IO_FILE *stdin;
基于这个'extern'关键字,我认为这只是一个声明.我不知道标准输入在哪里定义和初始化的?
Based on this 'extern' keyword, i assume this is just a declaration. I wonder where is stdin defined and initialized?
推荐答案
它在C库的源代码中定义.通常,您只需要标题即可进行编译,但是您可以找到许多开源标准库(例如glibc)的源代码.
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).
在glibc中,它在libio/stdio.c
中的定义如下:
In glibc, it's defined in libio/stdio.c
as like this:
_IO_FILE *stdin = (FILE *) &_IO_2_1_stdin_;
依次使用libio/stdfiles.c
中的宏定义哪个:
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);
DEF_STDFILE
宏的定义因几件事而异,但是它或多或少地使用文件描述符0
(这是Unix上的标准输入)来建立适当的FILE
结构.
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).
定义(可能会有所不同)取决于您的C库,当然也取决于平台.如果需要,可以继续围绕标准库I/O组件的各个部分进行操作.
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.
这篇关于C标准库中的stdin在哪里定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!