从输入数组存储数据 [英] Store data in array from input

查看:92
本文介绍了从输入数组存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我初学C.请不要介意我的问题是瘸子。在这个程序中,我已经写了,当我使用'for'循环第一次,我希望只有3值存储在一个数组,但它存储4个值,并在明年'for'循环如预期显示3的值。
我的问题是,为什么在'for'循环第一次需要4个值,而不是3?

 #包括LT&;&stdio.h中GT;
无效的主要()
{
    INT标记[3];
    INT I;    对于(I = 0; I&下; 3;我+ +)
    {
        的printf(请输入一个没有\\ n);
        scanf函数(%d个\\ N(引号+ I));
    }
    对于(I = 0; I&下; 3;我+ +)
    {
        的printf(%d个\\ N,*(标记+ I));
    }
}


解决方案

\\在 scanf函数 N 是问题

 #包括LT&;&stdio.h中GT;
诠释的main()
{
    INT标记[3];
    INT I;    对于(I = 0; I&下; 3;我+ +)
    {
        的printf(请输入一个没有\\ n);
        scanf函数(%D(引号+ I));
    }    的printf(\\ nEntered值:\\ n);
    对于(I = 0; I&下; 3;我+ +)
    {
        的printf(%d个\\ N,*(标记+ I));
    }    返回0;
}

原因:


  

我只希望 3 值存储在一个数组,但它存储4个值
  并在明年'for'循环如预期显示3的值。我的问题是,为什么
  在'for'循环第一次需要4个值,而不是3?


第一:否,它仅存储 3 号码,但没有 4 号码阵列标记[]

二:有趣的理解循环只运行了三次 I = 0 I< 3 。根据病情的for循环运行。更有趣的code卡在 scanf()的描述如下:

您的困惑就是为什么你必须输入四个数字,它不会因为你循环运行 4 倍,但其因为 scanf()的功能只有当你输入一个非空格字符(经过一番<大骨节病>输入 preSS你输入一个数字符号即非空格字符)返回。

要理解这种行为读手册: INT scanf函数(为const char *格式,...);


  

的空白字符(空格,制表符,换行符等;见A序列
   isspace为(3))。该指令的匹配任意数量的空格,
  包括无,在输入


由于在第一循环的,在 scanf()的您已经包括 \\ n 格式字符串,因此 scanf()的返回只有preSS一些<大骨节病>输入(或者非空格<大骨节病>键)。

  scanf函数(%d个\\ N(引号+ I));
           ^
           |
            新行字符

会发生什么?

假设输入到程序是:

  23℃---因为存储在标记[0]%D 23为I = 0
&LT;&进入GT; &LT; --- scanf函数消耗\\ N,还在第一循环
543&LT; --- scanf函数返回,并留下542条未读,
                               那么下一次迭代543读取的下一个迭代scanf函数
&LT;&进入GT;
193
&LT;&进入GT; &LT; --- scanf函数消耗\\ N,还在第3环
&LT;&进入GT; &LT; --- scanf函数消耗\\ N,还在第3环
123 LT; ---留在输入流中未读

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?

#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));
    }
}

解决方案

\n in scanf was the problem

#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 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?

First: No, it only stores 3 number but not 4 numbers in array marks[].

Second: interesting to understand loop runs only for three times for i = 0 to i < 3. The for loop runs according to condition. More interesting code is stuck in scanf() as described below:

Your confusion is why you have to enter four numbers, its not because you loop runs 4 times but its because scanf() function returns only when you enter a non-space char (and after some enter press you inputs a number symbol that is non-space char).

To understand this behavior read manual: 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.

Because in first for loop's, in scanf() you have included \n in format string, so scanf() returns only if press a number enter (or a non-space 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 

这篇关于从输入数组存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆