Newbie C Question from K&R
I been doing some C coding and have copied the following program from K&R. It compiles OK but doesn't do what it's supposed to - converting uppercase (from STDIN) to lowercase (to STDOUT).
Here's the code, Code:
|
There's a typo in this program, double check how 'c' variable is used.
|
Quote:
Code:
while (( c == getchar()) != EOF) Code:
while (( c = getchar()) != EOF) |
The original program was undoubtly correct. I'm not expecting Ritchie or Kernighan falling in such a newbie trap and publish buggy examples.
|
C example
If you post the output you get when you run the program against a small sample I may be able to help.
Reading the code you create an unitialised variable c, then in the while statement do a boolean comparison against getchar(), which is then compared to EOF to check for the end of file. At no stage is the value read in by stdin actually assigned to the variable c. Looking at the code I have to agree that using == to test against the getchar() function will result in unpredictable behaviour. Try changing it as kshkid suggested. I have run the program through VC7, and on debugging got an uninitialised variable error. Only way this program is going to run is if you change the code to read: while( (c = getchar()) != EOF) From experience we all make mistakes in write example code, and it is usually the simple things that get through as we presume they work and don't test. |
Quote:
Sorry once again. |
All times are GMT -5. The time now is 12:13 AM. |