Thread: Which Unix should I use while reading K&R ANSI?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    Which Unix should I use while reading K&R ANSI?

    I just picked up a copy of The C Programming Language by K&R. Which Unix should I use while studying C with this material?

    Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Any flavor of Unix or Linux should be fine. I find Unix/Linux a superb development environment, even if the learning curve is a bit steep. Your question is slightly off the mark though, since it's less the OS you're concerned with and more the compiler. Linux versions usually come with gcc and various Unixes may use gcc or their own brand. You could even program complete ANSI compliant C in Windows if you wanted. Many here use Pelles C (smorgasbordet - Pelles C) or MinGW (MinGW | Minimalist GNU for Windows), which are both free compilers for Windows.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    As a suggestion for Linux varieties, I would suggest either Fedora or Ubuntu, because they're simple to setup and lots of people use them so there is decent support.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    You're right Anduril, my post was pretty ambiguous. I was trying to use Visual Studio 2008 with K&R ANSI, but I was getting logical errors once some of the examples were compiled. Then I realized that the tutorial was intended for Unix users!

    I don't mind using a different OS or compiler. I would just wish that the compiler I use has the step in and step out features that VS has, which allows you to see your codes flow of execution one line at a time.

    I will check out these compilers you linked me! Thanks.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What kind of logical errors? I'm very curious as that really shouldn't be happening. I don't think there's anything in K&R that would only work on Unix, since ANSI C should be quite standard across platforms. Perhaps just a typo when you copied the code.

    As far as VS is concerned, there is most likely a setting somewhere to enforce strict ANSI C. Also, since VS is a C++ compiler by default, make sure you are setting it to compile C and not C++ or you may get tons of irrelevant errors.

    If you do go with Linux and gcc, you wont find a "built-in" debugger in the compiler (that would make it an IDE), but any Linux distro should give you gdb, a very powerful debugging program (again, somewhat steep learning curve, but there are good tutorials out there). There are also IDEs for Linux that probably provide this, I'm just not very familiar with them.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I believe Visual Studio is not ANSI C compliant and probably never will be.

    Just drop it and use Pelles or minGW if you are in Windows.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    The main problem that I'm having with Visual Studio 2008 and K&R's ANSI C is that the getchar() function never returns EOF(-1) as it reaches the end of a char, so the example programs starting by page 16 do not behave accordingly. The example program on page 16 uses the following loop condition:
    Code:
    while ((c = getchar() ) != EOF) {code to execute}
    I made the huge mistake of studying Tim Ward's online C tutorial for months, which is Dos and Applix specific. Now, I'm going to have to use completely different compilers, even though I've become accustomed to Visual Studio 2008.

    I'm a total noob.

    The font size in Pelles looks pretty small. I hope that I can change it because I'm as blind as a bat.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Another good IDE is Codeblocks which I believe comes with the minGW compiler. That's what I use for most of my C development.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by billybob2020
    The main problem that I'm having with Visual Studio 2008 and K&R's ANSI C is that the getchar() function never returns EOF(-1) as it reaches the end of a char, so the example programs starting by page 16 do not behave accordingly.
    The phrase "end of a char" does not make sense to me in this context. getchar() should return EOF when it attempts to read at the end of a file, or when EOF has otherwise been triggered, e.g., by entering CTRL + Z on Windows. EOF is a negative integer that is usually -1, but not guaranteed to be -1. Among other things, check that you are storing the result of getchar() in an int, not a char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    I installed Codeblocks with MinGw, and I still seem to be having the same problem. I will write the code and describe the problem.

    Code:
    #include <stdio.h>
    
    main
    {
    int c;
    
      while ((c = getchar() ) != EOF){
        putchar(c);
      }
    }
    The condition of this loop is that when getchar() returns EOF, it will terminate, so I'm inferring that getchar() is suppose to return EOF after reading the last char in the input buffer. Unfortunately, it is not returning EOF at the end of my characters.

    Is this some kind of error on my part, or must it be the compiler? Did I configure the compiler incorrectly?

    Any help to get me going so that I can continue my journey with K&R's ANSI C would be greatly appreciated!

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    To Laserlight,

    I apologize for the confusion. It is already 5am in my area, so I'm pretty exhausted. This is also pretty frustrating at this point.

    To explain where my confusion lies, to my knowledge, getchar() is incapable of reading characters or strings from a file. Therefore, I wouldn't think that the author was talking about reaching the EOF, but the end of a character or the end of a string.

    I'm basically inferring that when this sample was written, the author expected that the loop would terminate at the end of the string you inputted.

    I hope this clears things up.
    Last edited by cb0ardpr0gr^mm3r; 11-30-2010 at 05:01 AM.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The K&R was likely thinking of using pipes for input or the user sending the EOF.
    As stated above, EOF for windows is CNTL+Z this should send EOF to your program on standard input.

    Tim S.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by billybob2020
    To explain where my confusion lies, to my knowledge, getchar() is incapable of reading characters or strings from a file.
    Actually, it can, but indirectly. In particular, as stahta01 noted, you can redirect/pipe input from a file to your program, in which case the input would be effectively read from the file, and will trigger EOF as if you had used fgetc() instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Porting a large C project from unix to windows
    By somekid413 in forum C Programming
    Replies: 6
    Last Post: 09-23-2010, 03:24 PM
  2. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  3. Setting up a Unix box
    By @nthony in forum Tech Board
    Replies: 6
    Last Post: 07-22-2007, 10:22 PM
  4. Required Reading Unix Manual - Help!
    By wise_ron in forum Tech Board
    Replies: 3
    Last Post: 09-05-2006, 05:47 AM
  5. UNIX (Linux, BSD, etc) Programming :: UNIX
    By kuphryn in forum Linux Programming
    Replies: 6
    Last Post: 04-01-2004, 08:44 PM