Monday, July 2, 2012

A Fix for K & R Chapter 1

The Kernighan and Ritchie (K & R) book, The C Programming Language, 2nd Edition, has a trouble-some code snippet in chapter 1.    Many of the examples in chapter 1 have this test:

getchar() != EOF

This test might have worked 20 or 30 years ago, but it does not work well with modern keyboards.  If you code and run the examples with this code snippet, then to simulate the EOF character you have to use this combination in the Eclipse IDE:  Enter Control-z
In the Code Blocks IDE you need to do "Enter Control-z Enter"

You are better off modifying the program this way:

getchar() != '\n'

Now the program exits the while-loop when the Enter button is hit.

Robert

No comments:

Post a Comment

Comments require my approval before they appear. Be patient. It will take a day or two for your comment to appear.