我想了解为什么当我将 SIZE 定义为 2 时,程序允许我输入 3 个整数。当它返回数组时,它只返回两个数字,而不是我输入的三个数字。感谢您的帮助。
//C How to Program Exercises 2.23
#include <stdio.h>
#include <conio.h>
#define SIZE 2
int main (void){
int myArray[SIZE];
int count;
printf("Please enter 5 integers\n");
for (count=1;count<=SIZE;count++){
scanf("%d\n",&myArray[count]);
}
for (count=1;count<=SIZE;count++){
printf("The values of myArray are %d\n",myArray[count]);
}
getch();
return 0;
}
最佳答案
你的循环应该是
for (count=0;count<SIZE;count++)
数组索引是基于 C 的 0
。
由于 scanf()
调用中有一个空白字符 (\n
),因此它会等待您输入非空白字符来完成每个调用。删除 \n
:
for (count=0;count<SIZE;count++){
scanf("%d",&myArray[count]);
}
关于c - 为什么我的程序接受的整数太多而输入的整数太少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47041617/