本文介绍了你怎么看scanf函数,直到EOF用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个,但一旦到达应该EOF它只是重复这个循环和scanf一次。

  INT主要(无效)
{
        炭字[16];        而(scanf函数(%15秒字)== 1)
           的printf(%S \\ N字);        返回0;
}


解决方案

尝试:

 而(scanf函数(%15秒字)!= EOF)

您需要 scanf函数输出比较 EOF

由于您指定 15 格式字符串,你会读到最多15字符的宽度。所以的话字符数组应该是大小 16 15 +1 字符)。所以把它声明为:

 字符字[16];

I have this but once it reaches the supposed EOF it just repeats the loop and scanf again.

int main(void)
{
        char words[16];

        while(scanf("%15s", words) == 1)
           printf("%s\n", words);

        return 0;
}
解决方案

Try:

while(scanf("%15s", words) != EOF)

You need to compare scanf output with EOF

Since you are specifying a width of 15 in the format string, you'll read at most 15 char. So the words char array should be of size 16 ( 15 +1 for null char). So declare it as:

char words[16];

这篇关于你怎么看scanf函数,直到EOF用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 16:21
查看更多