为什么第一次输入后会关闭以下代码段?:
#include <stdio.h>
int main( ) {
int a;
int b;
printf( "Enter a first value :");
a = getchar( );
printf( "You entered: ");
putchar( a );
printf( "\n Enter a second value :");
b = getchar( );
return 0;
}
打印后程序关闭
"Enter a second value :"
答案 0 :(得分:1)
b = getchar( );
将b
分配给第一个输入的输入流中留下的换行符。
答案 1 :(得分:1)
检查getchar的返回
while (((b = getchar()) != '\n') && (b != EOF)) { }
检查它是否同时调用getchar
int main()
{
int a;
int b;
printf( "Enter a first value :");
while (((a = getchar()) != '\n') && (a != EOF)) { }
printf( "You entered: ");
putchar( a );
printf( "\n Enter a second value :");
while (((b = getchar()) != '\n') && (b != EOF)) { }
return 0;
}