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 ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    K&R Learning Issue

    Hello there,

    The following code appears in K&R (p.18) second edition;

    Code:
    main()
    {
        long nc;
    
        nc = 0;
        while (getchar() != EOF)
            ++nc;
        printf("%ld\n", nc);
    }
    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.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No need for linux.

    Just use the right EOF char - ctrl+z is the right one - has been so since DOS 3.1, at least.



    And hello from another Turbo C user (1.01).

    Elysia may need her smelling salts to revive her! Save the Pepto Bismol for Salem!

    Seriously, I use TC for lots of small and medium console programs - it's also handy for small, simple, graphics programs.


    Code:
    #include <stdio.h>
    
    int main()
    {
        long nc;
    
        nc = 0;
        while (getchar() != EOF)
            ++nc;
        printf("%ld\n", nc);
    
      printf("\n\n\t\t\t    press enter when ready");
      nc = getchar();
      return 0;
    }
    I use these last few lines for all my programs, to keep the console window from closing too quickly.
    Last edited by Adak; 09-04-2010 at 07:36 AM.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    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 07:30 AM. Reason: Added more pertinent info.

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Yes. EOF is a filesystem "condition". With console input, it can be simulated on a new line with Ctrl-Z followed by Enter.
    Code:
    > Quick red for jumps over the lazy brown dog.[CTRL-Z][Enter]    // Nothing will happen.
    > [CTRL-Z][Enter]        // Program should terminate.
    Let us how it goes.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    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:

    run.bat:
    Code:
    myapp.exe < data.txt
    pause
    Last edited by kmdv; 09-04-2010 at 07:35 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by rags_to_riches View Post
    ...

    EDIT: Beaten...but don't be bragging about your use of an old, outdated program no one should be learning to develop with.
    Did I copy his program into Notepad, save it, run it in Turbo C, and get back ALL BEFORE you had time to finish your post?

    THAT's why I like TC, in a nutshell: quick, simple, and to the point.

    Pelles C looks interesting though - just haven't tried it yet.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    A-ha!

    It works - excellent, thanks for that!

    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?


    EDIT - I should add I have Accelerated C++ for use after K&R.
    Last edited by TimHarrington; 09-04-2010 at 07:53 AM.

  8. #8
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Quote Originally Posted by TimHarrington View Post
    A-ha!

    It works - excellent, thanks for that!

    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.
    Last edited by Syscal; 09-04-2010 at 08:02 AM.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Clearly adak had never written prpgram that need heap more than 64kB?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  11. #11
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Quote Originally Posted by Adak View Post
    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.
    Code::Blocks!

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Bayint Naung View Post
    Clearly adak had never written prpgram that need heap more than 64kB?
    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.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Quote Originally Posted by Syscal View Post
    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.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Thanks for the post Elysia, is there a particular book you would suggest instead?

    I figure I should give K&R a go anyway, having spent the best part of 20 purchasing it, perhaps I will learn more changing their code to work on Windows (if needed) anyhow.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. On learning and games
    By Mario F. in forum General Discussions
    Replies: 24
    Last Post: 10-14-2009, 10:01 PM
  2. Some weird issue.
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 04-12-2009, 05:10 PM
  3. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 02:34 PM
  4. Is learning C/C++ worth it?
    By C/C++ Learner in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 11-05-2008, 03:55 PM
  5. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21