1

I have this program and I want it to increment by one and print the value of my counter each time I give a character

#include <stdio.h> int main(void){ //Declarations long nc; //Instantiations nc = 0; while (getchar() != EOF){ ++nc; printf("%ld\n", nc); } return 0; } 

When the loop initiates if I press ENTER I get 1,2,3,4,5... which is ok.But if I type a character or something else it prints the next two numbers 12,34,56,78. Why is that happening??

I am running the program on gcc 4.6.3 Ubuntu 12.04 release.

0

1 Answer 1

3

Terminal input is normally line buffered. Your program only gets input to process when you press ENTER. If you type several characters, you will get one line of output for each character you input (plus the newline itself) as getchar() returns each character in sequence.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer quite explanatory. I haven't noticed that if I write more characters I get more increments and prints.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.