You need to place the brackets as:
while( (ch = getchar()) != '\n')
Priority != Greater than =
while( ch = getchar() != '\n')
matches with:
while( ch = (getchar() != '\n') )
which reads char, compares it with a new line, and then assigns the comparison result to ch. Now the result of the comparison is 0 (when entering a new line) or 1 (when anything else is entered)
The strange char you see is a char control with a value of 1 , for ASCII 1 there is no character to print, so I assume that its shell prints a strange char with a value of 0001 .
You can confirm this by executing the output of your program in octal dump (od):
$ echo 'a' | ./a.out | od -bc
GCC when used with -Wall warns you how:
warning: suggest parentheses around assignment used as truth value
codaddict
source share