C: scanf to array

后端 未结 4 1258
野性不改
野性不改 2021-01-01 07:31

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.

Right now the code looks lik

4条回答
  •  攒了一身酷
    2021-01-01 07:52

    if (array[0]=1) should be if (array[0]==1).

    The same with 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 ==.

    = 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]);

提交回复
热议问题