You are not logged in.

#1 2009-01-10 10:46:48

cdwillis
Member
From: /home/usa
Registered: 2008-11-20
Posts: 294

The C Programming Language 2nd Edition by Kernighan & Ritchie

Ok, I've been interested in learning C for some time and this is the book that I have constantly seen recommended and it seems to considered the standard way most people have learned C. The thing is that I can't even compile the Hello World program from the introductory chapter. I did some google searching on other tutorials and found an alternate Hello World. This is the K&R version:

#include <stdio.h>
main()
{
  printf("hello, world\n");
}

Now here is the version that worked when I compiled it with gcc:

#include <stdio.h>
 
int main(void) {
   printf("Hello, world!\n");
   return 0;
}

Should I abandon learning C with the K&R book? If it leaves out something basic in the first program introduced how are the rest of the examples going to be? I'm somewhat bewildered now and any advice is appreciated (thanks in advance).

Offline

#2 2009-01-10 10:57:25

Lord Illidan
Member
From: Malta
Registered: 2007-10-25
Posts: 248

Re: The C Programming Language 2nd Edition by Kernighan & Ritchie

I tried out K&R's version, and it compiled and worked fine on my system, without using any special flags or whatever..
Does gcc return an error when you compile it?

EDIT : Could it be because you're telling gcc to treat all warnings as errors and using -Wall?

Last edited by Lord Illidan (2009-01-10 11:00:39)

Offline

#3 2009-01-10 11:03:02

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: The C Programming Language 2nd Edition by Kernighan & Ritchie

Both the K&R version and the other one compiled and worked for me. What errors did you get? How did you try to compile the K&R version?


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#4 2009-01-10 11:09:33

cdwillis
Member
From: /home/usa
Registered: 2008-11-20
Posts: 294

Re: The C Programming Language 2nd Edition by Kernighan & Ritchie

I just went and typed the K&R version out with nano, saved it, and it compiled and ran perfectly with gcc. I got errors when I compiled it in Geany earlier:

helloKnR.c:4: warning: return type defaults to 'int'
helloKnR.c:6: warning: control reaches end of non-void function

I guess they are warnings, not really errors.

Offline

#5 2009-01-10 11:35:28

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: The C Programming Language 2nd Edition by Kernighan & Ritchie

Of course, it explicitly says these are only warnings, so you can safely ignore them.
And you indeed need to use some options like -Wall to display these warnings, otherwise they are not printed.
Usually I prefer to make gcc happy, so I would use "int main(void)", but you should really ignore this for now and concentrate on the important stuff. Of course, K&R is still totally valid to learn C, these are just minor details. The only problematic section for me was the last one which use some very outdated linux system calls, which totally changed since then. But all the rest is fine.

Btw, c-faq can be very helpful when you run into some weird problems in C. For this question, I don't know if it is helpful, but at least it is funny :
http://c-faq.com/ansi/voidmainbooks.html


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#6 2009-01-10 20:49:25

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: The C Programming Language 2nd Edition by Kernighan & Ritchie

My opinion is that K&R is awesome, but is probably better as a reference than as a learning tool, especially for those learning their first few languages.

Offline

#7 2009-01-10 20:58:33

polarbear
Member
Registered: 2009-01-06
Posts: 8

Re: The C Programming Language 2nd Edition by Kernighan & Ritchie

K&R has to be my favorite book of all time. Oh, and another helpful resource: http://www.seebs.net/faqs/c-iaq.html .

Offline

#8 2009-01-10 21:35:59

marxav
Member
From: Gatineau, PQ, Canada
Registered: 2006-09-24
Posts: 386

Re: The C Programming Language 2nd Edition by Kernighan & Ritchie

Did you read paragraphs 1 and 2 od Chap 1, page 5...
"We are intentionally leaving out of this chapter features of C that are important [...].  This approach has its drawbacks [...] later chapters will necessarily repeat som of this chapter [...]"

Offline

Board footer

Powered by FluxBB