C语言 C: 扫描到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16299727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: scanf to array
提问by zergerge
I'm absolutely new to C, and right now I am trying master the basics and have a problem reading data from scanf straight into an array.
我对 C 完全陌生,现在我正在尝试掌握基础知识,但在将 scanf 中的数据直接读取到数组中时遇到了问题。
Right now the code looks like this:
现在代码看起来是这样的:
int main()
{
int array[11];
printf("Write down your ID number!\n");
scanf("%d", array);
if (array[0]=1)
{
printf("\nThis person is a male.");
}
else if (array[0]=2)
{
printf("\nThis person is a female.");
}
return 0;
}
As you can see, the program's aim is to ask for an ID, and determine from the first number whether the given the person is male(1) or female(2). However it seems I can't get it to work, because the array is not filled properly (this is checked via a printf(array) right after scanf, that results in random numbers). Running the program like this will give the result that the person is a male, no matter what number you read in.
如您所见,该程序的目的是请求一个 ID,并根据第一个数字确定给定的人是男 (1) 还是女 (2)。但是似乎我无法让它工作,因为数组没有正确填充(这是在 scanf 之后通过 printf(array) 检查的,这会导致随机数)。像这样运行程序将得出结果,无论您读入什么数字,该人都是男性。
So trivial it may seem, I couldn't figure out the problem.
看起来如此微不足道,我无法弄清楚问题所在。
回答by Maroun
if (array[0]=1)should be if (array[0]==1).
if (array[0]=1)应该是if (array[0]==1)。
The same with else if (array[0]=2).
与 相同else if (array[0]=2)。
Note that the expression of the assignment returns the assigned value, in this case if (array[0]=1)will be always true, that's why the code below the if-statement will be always executed if you don't change the =to ==.
请注意,赋值表达式返回分配的 value,在这种情况下if (array[0]=1)将始终为 true,这就是为什么如果不更改=to ,则 if 语句下方的代码将始终执行的原因==。
=is the assignment operator, you want to compare, not to assign. So you need ==.
=是赋值运算符,你要比较,而不是赋值。所以你需要==.
Another thing, if you want only one integer, why are you using array? You might want also to scanf("%d", &array[0]);
另一件事,如果您只想要一个整数,为什么要使用数组?你可能还想scanf("%d", &array[0]);
回答by hs.chandra
int main()
{
int array[11];
printf("Write down your ID number!\n");
for(int i=0;i<id_length;i++)
scanf("%d", &array[i]);
if (array[0]==1)
{
printf("\nThis person is a male.");
}
else if (array[0]==2)
{
printf("\nThis person is a female.");
}
return 0;
}
回答by unwind
The %dconversion specifier will only convert onedecimal integer. It doesn't know that you're passing an array, it can't modify its behavior based on that. The conversion specifier specifies the conversion.
该%d转换指定将只转换一个十进制整数。它不知道您正在传递一个数组,它无法基于此修改其行为。转换说明符指定转换。
There is no specifier for arrays, you have to do it explicitly. Here's an example with four conversions:
数组没有说明符,您必须明确地进行说明。这是一个包含四个转换的示例:
if(scanf("%d %d %d %d", &array[0], &array[1], &array[2], &array[3]) == 4)
printf("got four numbers\n");
Note that this requires whitespace between the input numbers.
请注意,这需要输入数字之间有空格。
If the id is a single 11-digit number, it's best to treat as a string:
如果 id 是单个 11 位数字,最好将其视为字符串:
char id[12];
if(scanf("%11s", id) == 1)
{
/* inspect the *character* in id[0], compare with '1' or '2' for instance. */
}
回答by Lingasamy Sakthivel
Use
用
scanf("%d", &array[0]);
and use ==for comparision instead of =
并==用于比较而不是=

