gpt4 book ai didi

c - 究竟什么是标准输入?

转载 作者:行者123 更新时间:2023-12-02 05:32:38 24 4
gpt4 key购买 nike

我对标准输入的实现有点困惑。究竟是什么?是指针吗?我尝试在 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");
}
}
当我输入

foobar1234567890


我得到结果:

a: foobar123

stdin: 4567890

Size of stdin: 8

This statement is always reached

This statement is also always reached


当我输入时

foobar


我得到输出

a: foobar

stdin:

Size of stdin: 4

This statement is always reached

This statement is also always reached


我知道 stdin 是一种 FILE 指针,但我不清楚。任何人都可以解释上述输出吗?

最佳答案

stdinFILE * 类型的指针。该标准不限制除此之外的实现,FILE 的详细信息完全取决于您的编译器。它甚至可能是不完整的类型(不透明)。

当然,尝试使用 FILE 打印 %s 会导致未定义的行为(除非 FILEchar * 或类似的类型定义,但几乎可以肯定不是)。

关于c - 究竟什么是标准输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44423865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com