该代码是检查是否有空行。我想当我输入一些文本时,它会继续执行 printf(),因为它卡在循环中。但事实上,它只执行一次 printf(),然后等待另一行文本。为什么?是因为在 gets() 函数之后输入将被删除吗?
这是代码
int main(){
char input[257];
char *ptr;
puts("Enter text a line at a time, then press Enter");
puts("Enter a blank line when done");
while( *(ptr= gets(input)) != NULL){
printf("You've entered: %s\n", input);
}
puts("Thank you and goodbye\n");
return 0;
}
最佳答案
此时应该可以解决问题
while( (ptr= gets(input)) != NULL && input[0]!='\0')
关于c - 使用 "while"检查是否有空行C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016200/