I have this following line of code:
while((temp = getchar())!= EOF){printf("Hello");}
I expected the program to print Hello for every char read, but it prints for every enter pressed instead. How can I change it to my intention?
I have this following line of code:
while((temp = getchar())!= EOF){printf("Hello");}
I expected the program to print Hello for every char read, but it prints for every enter pressed instead. How can I change it to my intention?
when you enter a character from the keyboard, every character goes into a buffer, including the enter(\n) character. getchar function will take each character in the buffer in turn and of course will include the enter character. You have to check the return character of getchar function is enter or not:
while ((temp = getchar()) != EOF) {
if (temp != '\n') {
printf("Hello\n");
}
}