我对stdin的实现有点困惑.究竟是什么?它是指针吗?我尝试在64位机器上使用sizeof打印stdin的大小并获得8.我甚至在%s说明符中使用*stdin取消引用它并在输入流中获取未使用的字符(所有这些).但是,如果我得到一个错误,我会在里面比较它的de引用值.我知道它是一个输入流.但它是如何实施的?
这是一个示例:
#include<stdio.h>
#include<stdlib.h>
int main(){
char a[10];
fgets(a,10,stdin);
printf("a: %s",a);
printf("\nstdin: %s",*stdin);
//if(*stdin=="foo"){
// printf("True");
//}//Error: invalid operands to binary == (have 'FILE' and 'char *')//and casting to char * doesn't help
printf("\nSize of stdin: %d\n",sizeof(stdin));
if(stdin!=NULL){
printf("This statement is always reached");//always printed even in when input is bigger than 10 chars and otherwise
}
if(!feof(stdin)){
printf("\nThis statement is also always reached");
}
}
Run Code Online (Sandbox Code Playgroud)
当我输入
foobar1234567890
我得到了结果:
a:foobar123
stdin:4567890
标准物的大小:8
始终达到此声明
始终达到此声明
当我输入
foobar的
我得到了输出
a:foobar
标准输入:
标准物的大小:4
始终达到此声明
始终达到此声明
我知道stdin是一种FILE指针,但我并不清楚.谁能解释上面的输出?
stdin是一个类型的指针FILE *.该标准不限制实现超出此范围,详细信息FILE完全取决于您的编译器.它甚至可能是不完整的类型(不透明).
当然,尝试打印FILEwith %s会导致未定义的行为(除非FILE是typedef char *或类似的,它几乎肯定不是).
stdin(标准输入的缩写)是一个指向FILE只能使用以下函数操作的类型的指针:
https://en.wikipedia.org/wiki/C_file_input/output
在大多数(如果不是全部)实现中,FILE 类型是具有整数文件描述符和字符缓冲区的结构,允许缓冲 I/O 操作。该文件描述符以只读模式打开,通常连接到终端、管道或文件。
还有stdout(标准输出)和stderr(标准错误),以写入模式打开。