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!
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!
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.
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.
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.
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.
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.
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:
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.Code:while ((c = getchar() ) != EOF) {code to execute}
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.
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.
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.Originally Posted by billybob2020
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I installed Codeblocks with MinGw, and I still seem to be having the same problem. I will write the code and describe the problem.
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.Code:#include <stdio.h> main { int c; while ((c = getchar() ) != EOF){ putchar(c); } }
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!
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.
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.
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.Originally Posted by billybob2020
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)