#include <stdio.h> void main(){ int c; while(c = getchar() != EOF){ putchar(c); } } 

I enter characters into the console, they are displayed normally. But as soon as I press Enter, i.e. I enter '\n' , besides switching to a new line in the console, the previous line is also duplicated.

snapshot

  • It should be so. What's next? - Qwertiy
  • Add brackets around c = getchar() . - αλεχολυτ

2 answers 2

I enter characters into the console

OK, you fill the standard input with characters. But they do not get anywhere. And as soon as Enter is pressed, they become available for reading, and your cycle (with correction) reads them from the standard stream and displays them. Everything works as it should.

 /* * Тут мы ввели строку "qwerty" и нажали Enter */ while( (c = getchar()) != EOF) { /* * А тут мы эту строку читаем по * одному символу и выводим: */ putchar(c); } 

    Input and output buffered . Therefore...

    One line - when you enter characters. They are not read until the Enter key is pressed. Clicked - the cursor moved to a new line, the read went from the buffer, and the output of all characters using putchar . This forms the second line.