Ins*_*per 2 c compilation getchar output
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a,b;
printf("enter the firstkeyword \n");
a = getchar();
printf("enter your second keyword \n");
b = getchar();
if(a>b)
{
printf("it is '%c' greater than '%c' as i expected \n",a,b);
}
else if (b>a)
{
printf("here'%c'is greater than '%c' \n",b,a);
}
else
{
printf("dont type the same character twice in next session \n");
}
return(0);
}
Run Code Online (Sandbox Code Playgroud)
在编译程序之后,o/p是:
输入第一个关键字
我输入'$'并使用ctrl + z来eof并'enter'继续该程序.但即使没有输入第二个关键字,编译器也会将输出打印为
输入第二个关键字
正如我所料,它比' - >'更大'.'
任何人都可以帮助这个计划吗?
对不起,如果有任何语法或短语错误.
getchar()\n当你按下enter哪个仍然存在于缓冲区时,也会采取额外的输入.你需要吸收这个额外的字符让第二个工作.getchar尝试getchar两次调用如下 -
char just_to_consume;
a = getchar();
just_to_consume = getchar();
printf("enter your second keyword \n");
b = getchar();
just_to_consume = getchar();
Run Code Online (Sandbox Code Playgroud)
除了上面的选项,你可以使用标准函数setvbuf来控制缓冲.还有一个选项(个人我不喜欢这个以避免未定义的行为)正在使用fflush(stdin)
| 归档时间: |
|
| 查看次数: |
155 次 |
| 最近记录: |