0
int kr=0;
int ss =0;
while ((kr=getchar()) != EOF){
      if(kr != '\n')
      {
        ss++;
      }

      printf("%d\n",ss);
}

With this code , printf is waiting until i press enter then printing all the sequential ss values at the same time like in this

enter image description here

. Can somebody explain this behavior ?

ΦXocę 웃 Пepeúpa ツ
  • 43,054
  • 16
  • 58
  • 83
Dogus Ural
  • 523
  • 8
  • 19

1 Answers1

5

printf is not waiting it is getchar instead. getchar uses a buffer behind the scene. When that buffer is empty, getchar will read 1 line from stdin and then return the first caracter. If it is not empty, it will return the next caracter from the buffer immediatly.

That means that the getchar will wait the first time you call it. And thus your printf is never executed until you press enter

litelite
  • 2,719
  • 4
  • 20
  • 32
  • what does this have to do with me being have to press enter everytime i want something printed. I dont understand your explanation – Dogus Ural Apr 28 '16 at 06:04
  • @DogusUral It means that the `getchar` in the condition of your loop is blocking until you press enter, the `printf` is never executed until you press enter. – litelite Apr 29 '16 at 19:50