Home > other >  Why does having scanf & printf in a loop cause this output
Why does having scanf & printf in a loop cause this output

Time:01-15

The program in question: #1:

int main(void) {
  int size;
  scanf("%d", &size);
  int v[size];
  for(int i = 0; i < size;   i) {
      scanf("%d", &v[i]);
      printf("the %d-th element is : %d\n",i, v[i]);
  }
  return 0;
}

O/P:

5
6 7 8 9 10 11
the 0-th element is : 6
the 1-th element is : 7
the 2-th element is : 8
the 3-th element is : 9
the 4-th element is : 10

My question: Why does it seem like the program does all the print statements after your input?

So does the program kind of 'hold' the print statements to display them after you press <Enter> for a new-line? Is there a better explanation of this behavior somewhere?

**Edit after @dbush's answer** I need a bit of a clarity on this: so let's say the `size = 5` & my input is `6 7 8 9 10` so I'm asuming the first `scanf` in the loop reads the `6 ` part then reads/stores the data and then passes the rest of the content that's in the input buffer to the next `scanf`'s leaves the rest of the input data in the `input buffer`

Then the program goes towards the printf statement stores that output in some sort of a buffer then it loops back to the 2-nd scanf and that reads the 7 part and all of this goes till the 10<enter> part, now the final scanf reads/stores 11 and since there's a newline, the programs decides that it no longer needs to read from the input buffer and now outputs the rest of the contents from the output buffer to the console.

Is this how it goes or are there some inaccuraries in this analogy?

Edit #2: I found this good blogpost explaining what goes on with scanf's & the input buffer, together with @dbush & @Weather Vane answers to completely answer any doubts I've had regarding this

CodePudding user response:

When reading from a terminal, nothing gets sent to scanf until you press ENTER.

So in your case, you have 6 strings which look like numbers in the input buffer. The first scanf reads just the first number, because that's what its pattern is looking for and leaves the rest in the input buffer. Then the program continues by calling printf, and because the string ends with a newline it gets printed immediately. When the program then loops back around to scanf, there's already data in the input buffer so it reads what's there and returns immediately, and the cycle continues.

So only the first call to scanf is waiting for input from the user, while the others are reading what's already in the input buffer, and the printf calls are having their output go to the terminal immediately since the string to print ends with a newline.

CodePudding user response:

When the program runs the first scanf("%d", &size); it reads the value of 5 from the input stream, and this value is used to define the size of the array "v". Then, the loop starts to run and for each iteration, it calls scanf("%d", &v[i]), which reads a value from the input stream and assigns it to the i-th element of the array.

However, it also calls printf("the %d-th element is : %d\n",i, v[i]), which prints the value of the i-th element of the array and the iteration number.

So, after the program reads the 5 values that it needs to fill the array, it reads one more value, which is not assigned to any variable, and this is the reason why you see the extra value in the output.

  • Related