I'd like to understand why the program allows me to input 3 integers when I have defined SIZE as 2. And when it returns the array it only returns two numbers NOT the three I have entered.Thanks for your help.
//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;
}
Your loops should be
Array indexing is
0
based in C.Since you have a whitespace chatacter (
\n
) inscanf()
call, it waits for you input a non-whitespace character to complete each call. Remove the\n
: