简体   繁体   English

C:scanf到数组

[英]C: scanf to array

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). 如您所见,该程序的目的是要求提供ID,并从第一个数字确定给定的人是男性(1)还是女性(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). 但是似乎无法使它正常工作,因为数组未正确填充(这是在scanf之后通过printf(array)检查的,这会导致随机数)。 Running the program like this will give the result that the person is a male, no matter what number you read in. 这样运行程序,无论您读多少书,该人都是男性。

So trivial it may seem, I couldn't figure out the problem. 看起来如此琐碎,我无法弄清楚问题所在。

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 == . 请注意,赋值的表达式返回分配的值 ,在这种情况下, if (array[0]=1) 始终为true ,这就是为什么如果不更改=则始终执行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]);

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

The %d conversion specifier will only convert one decimal integer. %d转换说明符将仅转换一个十进制整数。 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. 转换说明符指定转换。

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. */
}

Use 采用

scanf("%d", &array[0]);

and use == for comparision instead of = 并使用==进行比较,而不是=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM