Why does my program accept one integer too many and input one too few?

798 Views Asked by At

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

There are 3 best solutions below

0
On BEST ANSWER

Your loops should be

for (count=0;count<SIZE;count++)

Array indexing is 0 based in C.

Since you have a whitespace chatacter (\n) in scanf() call, it waits for you input a non-whitespace character to complete each call. Remove the \n:

   for (count=0;count<SIZE;count++){
    scanf("%d",&myArray[count]);
}
0
On

why the program allows me to input 3 integers

This loop runs exactly 2 times:

for (count=1;count<=SIZE;count++){
        scanf("%d\n",&myArray[count]);
    }

But as you used \n in the scanf(), this scanf() waits until you give any whitespace.

Proper Input code:

for (count=0;count<SIZE;count++){
        scanf("%d",&myArray[count]);
    }

And when it returns the array it only returns two numbers

Your original output code prints first number correctly, But your second number is garbage value.

Proper Output Code:

for (count=0;count<SIZE;count++){
        printf("The values of myArray are %d\n",myArray[count]);
    }

So Full Code goes like this:

//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=0;count<SIZE;count++){
        scanf("%d",&myArray[count]);
    }
    for (count=0;count<SIZE;count++){
        printf("The values of myArray are %d\n",myArray[count]);
    }
    getch();

    return 0;
}
0
On

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.