Download

c, getchar(), and Winbloze

I knew of no better place to ask, and I am probably going to go full newb here with this one.

Keep in mind…

  • I am using Windows.
  • I am using GCC.

Alright, to the fun part. If I use this, the while loop will never exit:


while((c = getchar()) c != EOF)


From what I can tell, this is due to getchar() absorbing the return key and is a consequence of using Windows, however if I do this instead:


while((c = getchar()) != '
' && c != EOF)


I will be good to go and things will work.

I don’t like this behaviour, and here comes the kicker, maybe I WANT to count the newline. I don’t want to use ANY other input method. Assume I want to use getchar(). Is there a way for me to count the
and have my while loop not stick? Maybe I am just being typical and looking over something simple?

Thanks!

Edit: NO, CtrlZ & F6 are not solutions!

No, after you press “return” you’ll receive return symbol too. Otherwise your != ’
way of terminating the loop wouldn’t work.

CtrlZ IS one of the solutions, because it sends EOF to the input stream.

Anwyay:



#include <stdio.h>

int main(){
	int i = 0;
	char c;
	while((c = getchar()) && (c != EOF)){
		i = c;
		printf("%d
", i); 
                /* do stuff here */
		if (c == '
')/* <== this */
			break;
	}	
}