Thread: learning c from old k&r code

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    53

    learning c from old k&r code

    i found these files and started to study and update /rewrite them to learn from it.
    V7/usr/src/cmd/yes.c

    i have however a question. i noticed there is no license .no comments, and besides from the old code it also uses many goto statements in some of the sources.

    i been under the impressing that only when writing on a kernel would ever need a goto in code and i also think from having read some other sources for the same programs from solaris and from gnu core-utils that these old codes seems to be not so well written and somehow very simple and i wonder if it is actual old production code.

    if anyone has similar links to older code or code like this i can "steal" and learn from i would be very happy. especially if the sources are available used for exact that purpose.

    thanks.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I wouldn't learn C from K&R C. You'll probably want to stick to modern code that adheres to a modern standard.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And further, what would the purpose of rewriting some already functional code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    And further, what would the purpose of rewriting some already functional code?
    You get to learn how old-timers used to do it!

    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    what would the purpose of rewriting some already functional code?
    I ask myself that every day when I come to work.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    You get to learn how old-timers used to do it!

    But why - modern code doesn't HAVE to fit in 64KB, or run quickly on a 4MHz/3-100 clocks per instruciton single issue pipeline processor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    But why - modern code doesn't HAVE to fit in 64KB, or run quickly on a 4MHz/3-100 clocks per instruciton single issue pipeline processor.
    My modern installation of windows with my modern software has trouble fitting on my modern hard drive, nor does it run quickly on my modern processor.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    Quote Originally Posted by MK27 View Post
    You get to learn how old-timers used to do it!

    exactly one reason why i want to do this.
    i have already taken the most simple programs and rewritten them and some of them a couple of times. wc as example i written more than one version of.

    i have also off course still good up to date books like advanced porgraming in the unix environment and online books and forums to use.

    i think it is great to have small programs to look at but i do however miss the option of using code from today standards but it seems to be bigger sources than i as beginner would start changing to much in.

    but this way i learn to update others work and as far as this is only hobby of mine it is not a great skill to have but still a good learning process.

    the thing is that i study unix systems as a complete personal interest and i will of course like to learn the tradition of programming for unix. in that context i do find these sources i linked to worth gold but i still would like to also get other input as well.

    thanks for reading the post.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by cmay View Post
    i been under the impressing that only when writing on a kernel would ever need a goto in code and i also think from having read some other sources for the same programs from solaris and from gnu core-utils that these old codes seems to be not so well written and somehow very simple and i wonder if it is actual old production code.
    Use of "goto" can sometimes be cleaner than introducing flag variables... but let's not start a huge anti-goto war here! There is nothing inherent in kernel code that would necessitate (or excuse) use of "goto" more than in any other context.

    And you're right. Core-utils and such are not necessarily well written. I'm gonna get in trouble for saying this but in my opinion, early internals code was not written by seasoned mainframe computer experts but rather by learn-as-you-go hackers who started with hobby computers and had hardly a clue about advanced data structures and multi-threaded code.

    I see crap all the time - for example, when examining some DNS domain resolving code which was supposed to be the type: "don't touch this: it's written by gurus" type code which was absolutely hideous. The code is in a production environment and expected to parse and search for 1000s of domain names a minute (second?). I had to get multi-colored markers out just to try and follow the umpteen-nested loops that spanned many pages. At least 10 colors.

    Gurus. Yeah right.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They were gurus because they didn't need markers.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nonoob View Post
    There is nothing inherent in kernel code that would necessitate (or excuse) use of "goto" more than in any other context.
    The ones I have seen in kernel related programming are actually pretty lame, eg:
    Code:
     
    somefunc () {
            if (err) goto finish_this;
            [...]
            finish_this: 
                 unregister_whatever();
                 return -1;
    }
    Which to me looks like pure style or your {} keys were temporarily disabled. So I agree with nonoob, there is no excuse.

    @cmay: glad to see we both see it is worth considering how things got to be the way they are...the geezurus are real people and add an element of "humanity" to the computer programming realm, methinks. I will never do anything that "big" -- and if I did, I would do it however the hell I want -- so maybe no excuse is enough ;0
    Last edited by MK27; 06-09-2009 at 05:44 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    37
    I'm not sure of your exact question. I learned C from K&R. These days I wouldn't learn it from a super old non-ansi K&R (my first C book) but if you like K&R I wouldn't hesitate to use the later editions. In fact the only book I own specifically on C is K&R and the only book I own specifically on C++ is Stroustrup.

    As far as gotos go, I have never used them much outside of very old BASIC and Fortran, but once in a while they may save you a couple of ifs. If you have a good reason, then use one. If you find some old code that helps you out, use that too.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    A wisely placed goto can be a great thing sometimes.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    Quote Originally Posted by SyntaxError View Post
    I'm not sure of your exact question. I learned C from K&R. These days I wouldn't learn it from a super old non-ansi K&R (my first C book) but if you like K&R I wouldn't hesitate to use the later editions. In fact the only book I own specifically on C is K&R and the only book I own specifically on C++ is Stroustrup.

    As far as gotos go, I have never used them much outside of very old BASIC and Fortran, but once in a while they may save you a couple of ifs. If you have a good reason, then use one. If you find some old code that helps you out, use that too.
    i think nonoob answered the question.

    i am learning c as a hobby and i study the old unix systems as i find them very interesting so what i do is i get some source code from books or online and i study those and then i try rewrite the code into my own version which is the todays standard c i use. i use the gcc std=99 flag when compiling on linux . and i use cc on open solaris for these old codes.

    i think its a great way to learn about things that has a interest and i think to have a whole unix version 7 in source to study and modify and rewrite is very rewarding to me.

    but i was sort of wondering if it really was old production code since some of it seems not very well written. i am very happy with the answers i got
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I learning out-dated code?
    By JustinAllard in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2005, 05:05 AM
  2. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 10:51 AM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM