getchar() != 在这段代码中有什么用?
What is the use of getchar() != in this piece of code?
在这一小段代码中不明白 getchar 的用途是什么。
他们要求用户输入一个 int 值,我必须检查它是否有效。我了解状态,它应该检查是否只输入了 1 个值。
提前致谢。
#include <stdio.h>
int main(void)
{
int x;
int status;
status = scanf("%i", &x);
if ((status != 1) || (getchar() != ’\n’)){
printf("\nUnvalid entry\n");
return 1;
}
printf("\nValid entry\n");
return 0;
}
getchar()
基本上检查字符。
getchar() != ’\n’
检查字符不是换行符。
您可以访问https://www.geeksforgeeks.org/difference-getchar-getch-getc-getche/
从中得到更多
您程序中的函数getchar()
确保用户只输入一个参数。
getchar()
要求输入的下一个字符。
因此,如果您输入 '1','2',[Space],[ENTER]
getchar returns char ' '
,在我们的例子中,这意味着我们有多个参数。
在这一小段代码中不明白 getchar 的用途是什么。 他们要求用户输入一个 int 值,我必须检查它是否有效。我了解状态,它应该检查是否只输入了 1 个值。
提前致谢。
#include <stdio.h>
int main(void)
{
int x;
int status;
status = scanf("%i", &x);
if ((status != 1) || (getchar() != ’\n’)){
printf("\nUnvalid entry\n");
return 1;
}
printf("\nValid entry\n");
return 0;
}
getchar()
基本上检查字符。
getchar() != ’\n’
检查字符不是换行符。
您可以访问https://www.geeksforgeeks.org/difference-getchar-getch-getc-getche/ 从中得到更多
您程序中的函数getchar()
确保用户只输入一个参数。
getchar()
要求输入的下一个字符。
因此,如果您输入 '1','2',[Space],[ENTER]
getchar returns char ' '
,在我们的例子中,这意味着我们有多个参数。