3
#include <stdio.h>
int main (void) 
{
  int T, y, z;
  scanf ("%i\n", &T);
  for (T; T > 0 ; --T) 
  {
   scanf ("%i\n", &y);
  }
return 0;
}

If I input 4, shouldn't it take 4 more inputs? Instead it allows me to enter 5 integers! Tried it for other numbers too.

reb94
  • 139
  • 1
  • 2
  • 8
  • 3
    Why not try to debug it? Print `T` at each iteration of the loop, and see how many iterations there are. – ugoren Jun 12 '13 at 10:42
  • 3
    first, you should not use a variable that is a upper letter. By convention it implies that T is a constant variable, whereas it is your iteration index... – zmo Jun 12 '13 at 10:43
  • @ugoren I had tried that before posting this question here but that output it gave was quite weird and I was unable to figure out where the problem was. I had added a `printf` statement after the second `scanf`. – reb94 Jun 12 '13 at 11:02

2 Answers2

9

Just get rid of \n from your scanf

Wintermute
  • 1,501
  • 17
  • 22
  • 2
    Just typing this up myself. It may seem like you're entering more values but its not reading them all. Next time go into debug mode and see the values you take in and the value of T to see what's happening, you'll learn more that way. – user1646196 Jun 12 '13 at 10:49
  • the "\n" will cause to ask a 1 more input after the end of the loop (+1) – MOHAMED Jun 12 '13 at 10:53
  • @user1646196 How to get into the debug mode? I am using MinGW compiler. I don't have an IDE. – reb94 Jun 12 '13 at 11:07
  • @MOHAMED I just noticed that even if I remove the newline character from the `scanf` statement, the cursor comes to the next line! This works only for the `scanf` statement and not for `printf`, right? – reb94 Jun 12 '13 at 11:14
  • 1
    Yes, because when you enter a variable for scanf you press enter which moves it to a new line. When it's just printing you don't need to hit enter to move on – user1646196 Jun 12 '13 at 11:20
9

The format string in scanf works as follows (see http://www.cplusplus.com/reference/cstdio/scanf/)

[The format is a] C string that contains a sequence of characters that control how characters extracted from the stream are treated:

Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character ...

In both your scanf() you have a newline. Therefore the first time you hit the enter key, it is ignored by scanf.

Some of the answers are telling you to modify the loop... this is incorrect, your loop is fine. it is the above that's causing you the headache. Try the following:

#include <stdio.h>
int main(int argc, char const *argv[])
{
  int T, y, z;
  scanf ("%i", &T);

  printf("Count is= %d\n", T);
  for (T; T > 0 ; --T) 
  {
      printf("T= %d\n", T);
      scanf ("%i", &y);
  }
return 0;
}

EDIT: Thank you to Daniel Fischer for his comment about flushing stdin, which I have now removed. Found this explanation (Using fflush(stdin)).

Community
  • 1
  • 1
Jimbo
  • 4,352
  • 3
  • 27
  • 44
  • 3
    `fflush(stdin)` is undefined behaviour. Emptying the input buffer is necessary here only if the input is malformed (`"12abc"` for example), since the `%i` conversion skips initial whitespace. If emptying is necessary, that should better be done in a portable way. – Daniel Fischer Jun 12 '13 at 11:00