stdin到底是什么?

维多·维斯特罗姆(Vidor Vistrom)

我对标准输入的实现有些困惑。到底是什么 是指针吗?我尝试在64位计算机上使用sizeof打印stdin的大小,并得到8。我什至在%s说明符中使用* stdin取消引用了它,并在输入流(所有字符)中得到了未使用的字符。但是,如果我收到错误,则在内部比较其取消引用的值。我知道它的输入流。但是如何实现呢?

这是一个示例:

#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");
}
}

当我输入

foob​​ar1234567890

我得到结果:

一个:foobar123

标准输入:4567890

标准输入大小:8

始终达到此声明

也总是这样的说法

当我输入

foob​​ar

我得到了输出

A:foobar

标准输入:

标准输入大小:4

始终达到此声明

也总是这样的说法

我知道stdin有点像FILE指针,但我不清楚。谁能解释以上输出?

MM

stdin是类型的指针FILE *该标准并不仅限于此,其详细信息FILE完全取决于编译器。它甚至可能是不完整的类型(不透明)。

当然,尝试打印FILEwith%s会导致不确定的行为(除非FILE是用于typedefchar *或类似的typedef ,但几乎可以肯定不是)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章