K&R Learning Issue

This is a discussion on K&R Learning Issue within the C Programming forums, part of the General Programming Boards category; Hello there,
The following code appears in K&R (p.18) second edition;
Code:
main()
{
long nc;
nc = 0;
while ...

K&R Learning Issue

On the various IDEs I have tried (on windows), the code doesn't work as intended, at least not afaik. It never displays the number of characters, because it never reaches "EOF". Note - I did change the main to return type int and then return 0 at the end on my own, in case anyone feels like pointing that out.

Now, having read around on the internet I noticed on stackoverflow that someone had a similar problem and replacing EOF with '\n' "solves" this issue, we now treat a newline as the end of input, which makes sense.. For the next program in the book this solution is also sufficient, but when we reach 1.5.3 we start line counting - now we can't use '\n' as K&R use that to count the number of new lines are being created and it wouldn't work anyway - it would always return '1'.

I have tried ^D as a way of sending an end of file command to the output window, as suggested elsewhere, but this hasn't worked. I guess(ed) this is the problem when learning from a very old book - but even after downloading Turbo C 2.01 (1989) I had the same issue, it wouldn't exit the while loop as there is never an 'EOF'.

So my question is this - what is the best IDE/compiler to use when reading K&R? I really want to run every program to help in my understanding, but if I keep running into these issues I am going to waste a lot of time trying to get even basic programs running and I assume this isn't an error in the book?

A short term solution to getting the line counting program up and running isn't really sufficient imo, I really just want to get going - have an IDE/compiler that will run every program in the book without issue...

...Do I need to install Linux or is it really a problem with the book?

Any help would be appreciated, I have searched high and low and spent a good few hours playing around with different compilers/IDEs that have been recommended only to get frustrated and forced to start a thread asking for some guidance.

K&R assumes the use of Unix, because that's where C originated. EOF on Windows is Ctrl-Z, perhaps followed by Enter (I don't have a Windows box available).
You can use any compiler...if you're on Windows, I would maybe suggest Pelles C for straight C development. You can use Visual Studio 2010 Express also, just make sure you create Console Applications and use .c as the file extension when creating new files.

EDIT: Beaten...but don't be bragging about your use of an old, outdated program no one should be learning to develop with.

Last edited by rags_to_riches; 09-04-2010 at 06:30 AM.
Reason: Added more pertinent info.

The code is absolutely valid and works as expected. The problem is you are using console to input text. In the console mode, it will not reach EOF, because you can still send characters to the input stream after pressing enter. If you press control+z it will print the number and terminate.

You can also save your data to a .txt file, create a .bat file, and redirect standard input:

Your replies bring me onto another quesiton though - should I be learning from K&R? Is this somewhat the 'long way round' to learning C? Ultimately I want to progress onto C++, having programmed Java at Uni at a simple/intermediate level (i.e. creating a league interface for football or simple robot programs) I realise I am a long way away from where I want to be, I have decent programming knowledge, but cannot say with any confidence that I am a good programmer in any one language. I guess you could say I learnt the "JavaSchools" way at Uni, although prior to Uni I did some Assembler, Pascal, C and C++ at college, albeit as a disinterested (unmotivated?) 17 year old.

Now I am 26 and just finished my MSc (in IS Management), I am highly motivated at the moment and have a few weeks off before I start work, I am looking to get into C/C++ in the meantime and read that K&R was the C bible.. Due to its age, is it perhaps not better to learn from a newer book? Or would the process of going from K&R C -> Modern C serve me well?

The job I start will involve programming as part of one of the 6 month rotations and I really want to be a developer/coder, mainly because I know I will enjoy it. They generally use all sorts of langauges, including C++, C and Java - they don't assume mastery of any of these for obvious reasons, but I still want to start with C so I can get a really good idea of what is going on. Speed is incredibly important in the industry - miliseconds matter, so C is still commonly used, certainly C++ too and Java is picking up from what I am told.

Sorry for typing out what seems like my life story, but I am a confused programmer who has dabbled with a lot of languages but isn't particularly proficient in any. My basic question is - will reading and learning from K&R give me a good base to learn other langauges from? Bearing in mind work will be sending me off on courses for other langauges at some point, a decent grasping of C basics can only serve me well?

Your replies bring me onto another quesiton though - should I be learning from K&R? Is this somewhat the 'long way round' to learning C? Ultimately I want to progress onto C++, having programmed Java at Uni at a simple/intermediate level (i.e. creating a league interface for football or simple robot programs) I realise I am a long way away from where I want to be, I have decent programming knowledge, but cannot say with any confidence that I am a good programmer in any one language. I guess you could say I learnt the "JavaSchools" way at Uni, although prior to Uni I did some Assembler, Pascal, C and C++ at college, albeit as a disinterested (unmotivated?) 17 year old.

Now I am 26 and just finished my MSc (in IS Management), I am highly motivated at the moment and have a few weeks off before I start work, I am looking to get into C/C++ in the meantime and read that K&R was the C bible.. Due to its age, is it perhaps not better to learn from a newer book? Or would the process of going from K&R C -> Modern C serve me well?

The job I start will involve programming as part of one of the 6 month rotations and I really want to be a developer/coder, mainly because I know I will enjoy it. They generally use all sorts of langauges, including C++, C and Java - they don't assume mastery of any of these for obvious reasons, but I still want to start with C so I can get a really good idea of what is going on. Speed is incredibly important in the industry - miliseconds matter, so C is still commonly used, certainly C++ too and Java is picking up from what I am told.

Sorry for typing out what seems like my life story, but I am a confused programmer who has dabbled with a lot of languages but isn't particularly proficient in any. My basic question is - will reading and learning from K&R give me a good base to learn other langauges from? Bearing in mind work will be sending me off on courses for other langauges at some point, a decent grasping of C basics can only serve me well?

I have that nice compact book. It's straight to the point and if I had not been familiar with C before reading it, I would have been totally lost. Even the way those exercises were worded would have confused the hell out of me had it not been to prior C knowledge. Also, I have had professors tell me that JAVA programmers is what a lot of people want. They would say things like "Why learn C? You will only likely use it when you support legacy applications". I'm still learning C and there seems to be a lot to STILL use it for. I can do some network programming, write a nice 6502 emulator, graphics, you name it. I would probably use JAVA to write draft code.

K&R second edition is still excellent for learning C. Concise and authoritative.

I suspect that C++ is more in demand on your job, than C, but that's just a guess. C++ is certainly more complex.

For serious C work, I would step up to the newer C compilers, however. TC is fine for little lessons and puzzles and all the basics, but you may need something that can more readily work with Windows, I suspect. Again, you'll know whether that is true or not, very soon.

MSVisual Express should be a good compiler/IDE candidate, if programming for Windows is involved, imo.

K&R second edition is still excellent for learning C. Concise and authoritative.

I suspect that C++ is more in demand on your job, than C, but that's just a guess. C++ is certainly more complex.

For serious C work, I would step up to the newer C compilers, however. TC is fine for little lessons and puzzles and all the basics, but you may need something that can more readily work with Windows, I suspect. Again, you'll know whether that is true or not, very soon.

MSVisual Express should be a good compiler/IDE candidate, if programming for Windows is involved, imo.

Only a few: My chess program, a sudoku program, and a program to organize the records of my folding team for Folding@Home.

For the first two, I used MSVisual Studio C/C++. For the latter program, I wrote up a multi-tiered merge sorting program to allow it to handle 500,000 records, efficiently. That allows it to generate it's reports in 10 seconds, using TC.

I'm not saying TC is the IDE or compiler I'd recommend most highly today. For Windows user's, that would be MSVisual Express or Studio. Not sure what's best for Linux.

I've heard good things about Code::Blocks, but haven't personally used it.

I have that nice compact book. It's straight to the point and if I had not been familiar with C before reading it, I would have been totally lost. Even the way those exercises were worded would have confused the hell out of me had it not been to prior C knowledge. Also, I have had professors tell me that JAVA programmers is what a lot of people want. They would say things like "Why learn C? You will only likely use it when you support legacy applications". I'm still learning C and there seems to be a lot to STILL use it for. I can do some network programming, write a nice 6502 emulator, graphics, you name it. I would probably use JAVA to write draft code.

Thanks

That is what I thought looking through the book, a bit confusing without getting involved with C first. Not only that, but from what I read it seems C is great to learn, so I will continue on with my studying of K&R

I don't mind the idea of using Java, but having read http://www.joelonsoftware.com/articl...vaSchools.html I really feel I need to learn C to further develop my understanding of computing. I have Visual C++ 2010 Express, so I will move onto that and use that for the rest of the book.

Any book that tries to learn implicit main, fail to return 0 at the end and assumes linux is a book that needs to be dumped and burned.
C is a platform independent language. Therefore, a book should not assume any particular platform.
It is also important to know safe and modern C programming instead of outdated syntax and idoms. Therefore, I recommend that you dump that book.

As for the learning goes, you can learn the basics of programming with any language you want. This is the same for every language. The syntax, the logic, algorithms, etc.
Then comes the actual idoms of the language, syntax and stuff. You won't be doing yourself a favor by learning C before C++ here. That applies to any other language, of course. Because languages tend to be radically different. That's why it's usually recommended that people learn the language they want to learn in the end and not some intermediate language (such as people learning C before C++). Obviously, since you want to learn not one language, but several, this does not apply to you. I should just say that you should learn them in any order you want.

Adak: Have you tried using Code::Blocks for your small projects? It's fast, snappy and the compiler GCC isn't very slow, either. If those are the reasons you still use TC, then perhaps you can find an alternative that's modern. Not saying you should, but perhaps you should try it and see if you like it.
Visual Studio is arguably fat, bloated and slow. Unfortunately. But perhaps C::B might be suited for your small projects.