getchar()或putchar()一直在吃我输入的第一个字符 [英] getchar() or putchar() keeps eating the first character of my input

查看:851
本文介绍了getchar()或putchar()一直在吃我输入的第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:此问题已解决。谢谢您的所有答复

这是我的程序:

#include <stdio.h>
int main(){

printf("write something : \n");
int c = getchar();

while((c = getchar()) != EOF){

if (c == ' ' || c == '\t')
 printf(" \n");
else
  putchar(c)
}
return 0;
}

每次我运行它时,它都能正常工作,但会吃掉我的第一个字符输入
例如,当我运行程序时,输出看起来像这样:

everytime i run it, it works fine, but eats the first character of my input for example when i run the program the output looks like this:

write something : 
this is a sentence.
his 
is
a
sentence.

缺少 t。
为什么会这样,我该如何解决?

the "t" is missing. why is that happening and how can i fix it?

谢谢您的时间。

推荐答案

您说的 int c = getchar()它将检索 t

然后,当您说 while(c = getchar())时,它将检索 h ,请注意,由于在期间 getchar ,您甚至没有机会打印出该字符

You say int c = getchar() which will retrieve "t".
Then when you say while (c = getchar()) it will retrieve "h", note that you did not even get a chance to print the character out since you called getchar in the while statement.

要解决此问题,请声明 int c = 0; int c ;

To fix this, declare int c = 0; or int c;

然后在 getchar() > while 循环时,您将从第一个字符开始。

Then when you call getchar() in the while loop you will start at the first character.

这篇关于getchar()或putchar()一直在吃我输入的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆