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 ha
C arrays are indexed starting from 0
, not from 1
. C does not automatically perform bounds checking on array accesses, and indeed, your code is well-formed. However, its runtime behavior is undefined on account of its using an array element expression to write outside the bounds of that array, and, separately, on account of its using an array element expression to read outside the bounds of that array.
Inasmuchas the program definitely exhibits undefined behavior on every run, absolutely nothing can be said about what it should do. If in practice you observe the input loop iterating thrice, then a likely explanation is that the second iteration overwrites the value of the count
variable. Given the order in which the variables are declared, this is a plausible manifestation of the undefined behavior in play.
The output loop, on the other hand, iterates exactly the number of times you told it to do: once with count == 1
, and once more with count == 2
. This is by no means guaranteed given the general undefinedness of an execution of your program, but it is about the least surprising behavior I can think of.