[英]C Scanf suddenly stopped reading in values
I'm trying to run a simple C program on my Mac. 我正在尝试在Mac上运行一个简单的C程序。 It worked fine for a while but then suddenly scanf stopped working.
它工作了一会儿,但是突然scanf停止了工作。 I basically want to read in an integer value and output what was entered.
我基本上想读取一个整数值并输出输入的内容。 No matter the integer I enter the program keeps outputting 0. I've tried the suggestions here but nothing works.
无论我输入的整数是什么,程序始终输出0。我在这里尝试了建议,但没有任何效果。 I've tried running the program in both terminal and xcode but still nothing.
我试过在终端和xcode中都运行该程序,但还是没有。 Any ideas?
有任何想法吗?
#include <stdio.h>
int main(){
int numberOfElements = 0;
scanf("Number of elements: %d",&numberOfElements);
printf("%d\n",numberOfElements); //keeps returning 0 no matter the number I enter
return 0;
}
You have to print the prompt, and only scan the input: 您必须打印提示,并且仅扫描输入:
printf("Number of elements: ");
fflush(stdout);
scanf("%d", &numberOfElements);
Generally, it is better to avoid using scanf()
directly. 通常,最好避免直接使用
scanf()
。 Instead, you can obtain a line of input and then use sscanf()
on the retrieved string for better control of errors. 相反,您可以获取一行输入,然后对检索到的字符串使用
sscanf()
以更好地控制错误。
char line[MAX_LINE];
if (fgets(line, sizeof(line), stdin) != NULL) {
if (sscanf(line, "%d", &numberOfElements) != 1) {
printf("You didn't input a number: %s", line);
/* ... */
}
}
You should not have Number of elements:
in the scanf
statement. 您不应在
scanf
语句中具有Number of elements:
scanf
You want to change the scanf
statement to: 您要将
scanf
语句更改为:
printf ( "Number of elements: " );
scanf ("%d", &numberOfElements );
When you include any non-format characters into scanf/fscanf/sscanf
format string, it means that you require these characters to be present in the input stream (or input string, in case of sscanf
). 当在
scanf/fscanf/sscanf
格式字符串中包含任何非格式字符时,这意味着您要求这些字符出现在输入流中(如果是sscanf
, sscanf
输入字符串)。 If these characters are not present in the input, scanf
fails. 如果输入中不存在这些字符,则
scanf
失败。 Space character has special meaning though, causing scanf
to skip all whitespace, but not requiring any whitespace to be present. 尽管空格字符具有特殊含义,但会导致
scanf
跳过所有空格,但不需要任何空格。
So, in order for this scanf
to succeed 因此,为了使此
scanf
成功
scanf("Number of elements: %d", &numberOfElements);
you have to enter, literally 你必须输入
Number of elements: 10
or 要么
Numberof elements:10
or 要么
Numberofelements: 10
or something like that. 或类似的东西。 Ie you have to type in the words
Number
, of
, elements
and the :
as well. 也就是说,您还必须输入单词
Number
, of
, elements
和:
。 If you just enter 10
, the scanf
will fail, since a mere 10
does not match the requested format. 如果仅输入
10
,则scanf
将失败,因为仅有10
与请求的格式不匹配。
I'm sure this was not your intent. 我确定这不是您的意图。 What you really wanted to do, apparently, is to print the prompt using
printf
and then do a simple 显然,您真正想做的是使用
printf
打印提示,然后做一个简单的
scanf("%d", &numberOfElements);
The aforementioned feature of scanf
could be useful, but probably only in some rare cases. scanf
的上述功能可能很有用,但可能仅在少数情况下有用。 This means that when you see any non-format text in scanf
format string, there's a good possibility that something is wrong. 这意味着当您看到任何
scanf
格式字符串的非格式文本时,很可能出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.