problem with printing char in VS console mode.

Hello Every body,
Trying to display an input to output screen , I always get the same character no matter what I have typed.

1
2
3
4
5
6
7
8
9
#include <stdio.h>

void main(void){

char ch;
 while(ch=getchar()!=EOF){
	printf("%d",ch); // Always outputs 11
	}
 }


the printf function always prints 11 on the console output.I am running and compiling the above with MS VS 2008 under Win Vista.(I know its weired running such an old fashioned piece of code with this machine..)
I know its weired running such an old fashioned piece of code with this machine.
I take offense to that, sir!

There's a problem with your condition. the assignment operator has a lower precedence than the inequality operator, so your expression is equivalent to ch=(getchar()!=EOF), not (ch=getchar())!=EOF.
Suppose you typed a<enter>. The first 1 comes from 'a'!=EOF being true, the second comes from '\n'!=EOF being true.
Thanks for your note now it works!

I didnt mean to offend BTW, What was offensive with that ?

Us backend programmers find it offensive to imply that console interfaces are obsolete. They are not, and you should never listen to anyone who tells you otherwise. They are just unpopular with the current generation of casual computer users.
Great/Horrible programs either are great/horrible regardless of their interface, or how they take advantage of their particular interface is part of what makes them great/horrible.
Last edited on
Topic archived. No new replies allowed.