C语言 将输入的数据存储在数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17831060/
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
Store data in array from input
提问by ranaarjun
I'm beginner in C. Please dont mind if my question is lame. In this program which i have written, when I use 'for' loop first time , I expect only 3 values is stored in an array but it stores 4 values and in next 'for' loop as expected show 3 values. My Question is why in 1st 'for' loop it takes 4 values instead of 3?
我是 C 初学者。请不要介意我的问题是否蹩脚。在我编写的这个程序中,当我第一次使用 'for' 循环时,我希望只有 3 个值存储在一个数组中,但它存储了 4 个值,并且在下一个 'for' 循环中按预期显示了 3 个值。我的问题是为什么在第一个“for”循环中它需要 4 个值而不是 3 个?
#include<stdio.h>
void main()
{
int marks[3];
int i;
for(i=0;i<3;i++)
{
printf("Enter a no\n");
scanf("%d\n",(marks+i));
}
for(i=0;i<3;i++)
{
printf("%d\n",*(marks+i));
}
}
回答by Sanyam Goel
\nin scanfwas the problem
\n在scanf是问题
#include<stdio.h>
int main()
{
int marks[3];
int i;
for(i=0;i<3;i++)
{
printf("Enter a no\n");
scanf("%d",(marks+i));
}
printf("\nEntered values:\n");
for(i=0;i<3;i++)
{
printf("%d\n",*(marks+i));
}
return 0;
}
Reason:
原因:
I expect only
3values is stored in an array but it stores 4 values and in next 'for' loop as expected show 3 values. My Question is why in 1st 'for' loop it takes 4 values instead of 3?
我希望只有
3值存储在数组中,但它存储 4 个值,并在下一个“for”循环中按预期显示 3 个值。我的问题是为什么在第一个“for”循环中它需要 4 个值而不是 3 个?
First:No, it only stores 3number but not 4numbers in array marks[].
第一:不,它只存储3数字而不是4数组中的数字marks[]。
Second:interesting to understand loop runs only for three times for i = 0to i < 3. The for loop runs according to condition. More interesting code is stuck in scanf()as described below:
第二:有趣的理解循环只运行三次i = 0到i < 3。for 循环根据条件运行。更有趣的代码scanf()如下所述:
Your confusion is why you have to enter four numbers, its not because you loop runs 4times but its because scanf()function returns only when you enter a non-space char (and after some enterpress you inputs a number symbol that is non-space char).
您的困惑是为什么您必须输入四个数字,这不是因为您循环运行4次数,而是因为scanf()函数仅在您输入非空格字符时才返回(并且在enter按下一些键后您输入了一个非空格字符的数字符号)。
To understand this behavior read manual: int scanf(const char *format, ...);:
要了解这种行为,请阅读手册int scanf(const char *format, ...);:
A sequence of white-space characters (space, tab, newline, etc.; see
isspace(3)). This directive matches any amount of white space, including none, in the input.
一系列空白字符(空格、制表符、换行符等;请参阅
isspace(3))。该指令 匹配 input 中任意数量的空格,包括无空格。
Because in first for loop's, in scanf()you have included \nin format string, so scanf()returns only if press a number enter(or a non-space key).
因为在第一个 for 循环中, scanf()您已包含\n在格式字符串中,因此scanf()仅在按下数字enter(或非空格key)时才返回。
scanf("%d\n",(marks+i));
^
|
new line char
What happens?
发生什么了?
Suppose input to program is:
假设程序的输入是:
23 <--- because of %d 23 stored in marks[0] as i = 0
<enter> <--- scanf consumes \n, still in first loop
543 <--- scanf returns, and leave 542 unread,
then in next iteration 543 read by scanf in next iteration
<enter>
193
<enter> <--- scanf consumes \n, still in 3rd loop
<enter> <--- scanf consumes \n, still in 3rd loop
123 <--- remain unread in input stream
回答by Provessor
remove \nand ican be created in the if statement as for (int i = 0; i < 3; i++) {}
删除\n并i可以在 if 语句中创建为for (int i = 0; i < 3; i++) {}

