如何在C中扫描单个字符
在C中: 我正在尝试使用如何在C中扫描单个字符,c,char,scanf,C,Char,Scanf,在C中: 我正在尝试使用scanf从用户那里获取char,当我运行它时,程序不会等待用户键入任何内容 代码如下: char ch; printf("Enter one char"); scanf("%c", &ch); printf("%c\n",ch); 为什么不工作?转换说明符不会自动跳过任何前导空格,因此如果输入流中有一个错误的换行符(例如,来自上一个条目),scanf调用将立即使用它 解决此问题的一种方法是在格式字符串中的转换说明符之前放置一个空格: scanf(" %c",
scanf
从用户那里获取char,当我运行它时,程序不会等待用户键入任何内容
代码如下:
char ch;
printf("Enter one char");
scanf("%c", &ch);
printf("%c\n",ch);
为什么不工作?转换说明符不会自动跳过任何前导空格,因此如果输入流中有一个错误的换行符(例如,来自上一个条目),
scanf
调用将立即使用它
解决此问题的一种方法是在格式字符串中的转换说明符之前放置一个空格:
scanf(" %c", &c);
格式字符串中的空白告诉
scanf
跳过前导空格,第一个非空格字符将用%c
转换说明符读取 在扫描输入前fflush(标准输入)代码>以清除缓冲区 在%c转换说明符之前提供空格,以便编译器忽略空格。该程序可编写如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
printf("Enter one char");
scanf(" %c", &ch); /*Space is given before %c*/
printf("%c\n",ch);
return 0;
}
#包括
#包括
int main()
{
char ch;
printf(“输入一个字符”);
scanf(“%c”,&ch);/*空格在%c之前*/
printf(“%c\n”,ch);
返回0;
}
我想和大家分享一个类似的东西
在Visual Studio上工作时,可能会出现如下错误:
“scanf”:函数或变量可能不安全。请考虑使用<代码> Snffys。要禁用弃用,请使用\u CRT\u SECURE\u NO\u警告
为了防止这种情况,您应该使用以下格式编写它
单个字符可按如下方式读取:
char c;
scanf_s("%c", &c, 1);
读取非空终止字符串的多个字符时,整数用作宽度规范和缓冲区大小
char c[4];
scanf_s("%4c", &c, _countof(c));
使用字符串而不是char
像
我相信它很好用。首先,避免scanf()
。使用它是不值得的痛苦
见:
在scanf()
中使用空白字符会忽略输入流中剩余的任何数量的空白字符,如果需要读取更多输入,该怎么办?考虑:
#include <stdio.h>
int main(void)
{
char ch1, ch2;
scanf("%c", &ch1); /* Leaves the newline in the input */
scanf(" %c", &ch2); /* The leading whitespace ensures it's the
previous newline is ignored */
printf("ch1: %c, ch2: %c\n", ch1, ch2);
/* All good so far */
char ch3;
scanf("%c", &ch3); /* Doesn't read input due to the same problem */
printf("ch3: %c\n", ch3);
return 0;
}
#include <stdio.h>
int main(void)
{
int i;
while(1) {
if (scanf("%d", &i) != 1) { /* Input "abc" */
printf("Invalid input. Try again\n");
} else {
break;
}
}
printf("Int read: %d\n", i);
return 0;
}
#include <stdio.h>
int main(void)
{
int age;
char name[256];
printf("Input your age:");
scanf("%d", &age); /* Input 10 */
printf("Input your full name [firstname lastname]");
fgets(name, sizeof name, stdin); /* Doesn't read! */
return 0;
}
另一个常见问题是混合使用scanf()
和fgets()
。考虑:
#include <stdio.h>
int main(void)
{
char ch1, ch2;
scanf("%c", &ch1); /* Leaves the newline in the input */
scanf(" %c", &ch2); /* The leading whitespace ensures it's the
previous newline is ignored */
printf("ch1: %c, ch2: %c\n", ch1, ch2);
/* All good so far */
char ch3;
scanf("%c", &ch3); /* Doesn't read input due to the same problem */
printf("ch3: %c\n", ch3);
return 0;
}
#include <stdio.h>
int main(void)
{
int i;
while(1) {
if (scanf("%d", &i) != 1) { /* Input "abc" */
printf("Invalid input. Try again\n");
} else {
break;
}
}
printf("Int read: %d\n", i);
return 0;
}
#include <stdio.h>
int main(void)
{
int age;
char name[256];
printf("Input your age:");
scanf("%d", &age); /* Input 10 */
printf("Input your full name [firstname lastname]");
fgets(name, sizeof name, stdin); /* Doesn't read! */
return 0;
}
如果inut缓冲区中有足够的空间,则使用fgets()
时需要注意的一个细节将在换行符中读取。如果不需要,您可以删除它:
char line[256];
if (fgets(line, sizeof line, stdin) == NULL) {
printf("Input error.\n");
exit(1);
}
line[strcpsn(line, "\n")] = 0; /* removes the trailing newline, if present */
尝试使用getchar();反而
语法:
void main() {
char ch;
ch = getchar();
}
这对我有用试试看
int main(){
char c;
scanf(" %c",&c);
printf("%c",c);
return 0;
}
fgets和getchar都不能解决这个问题。
唯一的解决方法是在使用scanf时在%c之前保留一个空格
scanf(“%c”,ch);//只会起作用
在以下情况下,FGET也不起作用
char line[256];
char ch;
int i;
printf("Enter a num : ");
scanf("%d",&i);
printf("Enter a char : ");
if (fgets(line, sizeof line, stdin) == NULL) {
printf("Input error.\n");
exit(1);
}
ch = line[0];
printf("Character read: %c\n", ch);
必须使用有效的变量ch
不是此程序的有效变量。使用字符Aaa
char aaa;
scanf("%c",&Aaa);
经过测试,可以正常工作。唯一适合我的代码是:
scanf(" %c",&c);
我也有同样的问题,而且只有一个字符。经过一个小时的随机测试,我还不能报告任何问题。有人会认为C现在已经具备了从键盘检索单个字符的防弹功能,而不是一系列可能的破解方法。。。只是说…这是约翰·博德两年前提出的同一个建议。这并不能回答问题,因为OP希望扫描一个字符,而不是一系列字符。fflush(stdin)根据C标准,代码>是未定义的行为。这是不正确的,fflush(stdin)
是UB,在SO的很多问题中都提到过。它与Microsoft编译器一起工作,但这只是为了澄清一下,如果C程序只包含上面的代码,它将按预期工作。由于P.P的回答中提到的原因,只有当与其他I/O代码一起使用时,才会出现像OP提到的一个这样的潜在问题。“ch不是此程序的有效变量”,除非它是?这是给定示例中的第一行代码。这究竟是如何试图为旧的、现有的答案增加价值的?实际上,在阅读了整个线程之后,我不得不得出结论,这里没有确定的答案。。。只是权宜之计雪太野了!你能解释一下为什么%c
前面的空格会起作用吗?已解决:这似乎不会比2012年的公认答案添加更多信息。这似乎对我不起作用。Deitel0805.exe中0x799AF2F6(ucrtbased.dll)处未处理的异常:向认为无效参数致命的函数传递了无效参数#include#include int main()