Hello, I am reading the K & R C book, and trying to understand more about putchar()/getchar() by practice. Similar to the exercise 1-12, I want try to parse each line by character. Although there are many site about the exercise, but I could find similar case of mine, which is similar to fold command. Not sure it's my code problem, or from my display setting.
#include <stdio.h>
/*count the characters in input; */
main() {
char c;
while (c = (getchar() != EOF) )
{
// putchar(c);
printf("%c\n", putchar(c));
}
}
Input:
A test
And I am expecting output like:
A
t
e
s
t
But I got strange char on the screen, can't be pasted here. What did I miss? Thanks!
No, still not working. There is no error at compiling, and running of the compiled program seems working too, except the char are not corrected printed out. E.g:
$ ./print_by_char
A test
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
$
Those [ ] on my screen are tiny squares with 0001 inside like.
|00|
|01|
I thought it may be related to display or language setting that I have no idea about, but testing of some other code from the book were fine. I am using Ubuntu 12.10 with bash. $LANG=en_CA.UTF-8.
Scott, it seem the syntax is correct.
Any other clue? Thanks!
The O's and 1's are the false's and true's that getchar() != EOF is returning into c because your condition is wrong.
$ cat main.c
#include <stdio.h>
/*count the characters in input; */
int main() {
char c;
while ((c = getchar()) != EOF)
{
printf("%c\n", c);
}
return 0;
}
$ cc -o main main.c
$ ./main
Hello World
H
e
l
l
o
W
o
r
l
d
Remember that the "c" variable is an int variable. You are using it to store what we call a "character", but ultimately "c" just holds a number.
What number does it store?
Under the "good" logic, it stores ASCII decimal 65 for 'A', etc. for each character it reads. That is fine.
Under the "bad" logic, it stores a 0 or 1 (equal or unequal). What ASCII characters do 0 or 1 represent? Some weird control character. That's why you get the garbage printed.
That's why it's a bad idea to perform assignments inside the conditional clause of thing like an if statement or while loop.
So what if it saves a line or two of code. Writing quality code is not a contest to see who can stuff the most operations in the least number of lines. Especially given that you wind up with bug-prone and hard-to-understand code.
I wouldn't worry about it too much. You're doing fine, it just takes a while when you are learning.
I agree with achenlee that there is too much obscure C code, with stuff jammed on one line. I don't write that way. But you see it a lot. Even K&R frequently emphasizes that style. Many expert programmers are used to that kind of terse code, and seem to like it. So you have to also learn to understand it.