This is what happens in your program
int c;
reserve space for an int
(call that space c
) and don't worry about its contents.
while(c = getchar( ) != EOF)
The thing in parenthesis can be written as c = (getchar( ) != EOF)
because the assignment operator has lower precedence than the inequality operator.
getchar()
waits for a keypress and returns the value of the key pressed
- That value is checked against EOF
- As it's different, the result of the inequality operator is
1
- and the value
1
gets put in the space with name c
.
Then, inside the while loop
{
putchar(c);
}
you're printing the character with value 1. As you've noticed, on your computer, the character with value 1 does not have a beautiful format when displayed :)
If you really want to print the values 0
or 1
with beautiful formats, try this
c = 0; /* or 1 */
putchar('0' + c);
If you want to print a value between 0 and 9 as a character, try this
c = 5; /* or 7, or 0, ... */
putchar('0' + c);