OpenBSD Journal

[Ask OBSDJ] Examples for Beginning C Programmer?

Contributed by Dengue on from the ask dept.

An Anonymous Coward asks:
I am trying to learn programming in C. My end goal would be to help code things for OpenBSD/Linux/Solaris (Basically POSIX stuff.) What are some resources the OpenBSD community can recommend for a newbie C/unix coder? (I am especially interested in network related stuff) I have currently been practicing under my OpenBSD box. What would be some good source (since we have access to all the OpenBSD source) code to look at that would not be too confusing for a beginner?

(Comments are closed)


Comments
  1. By Sean Cody () null@tfh.ca on http://www.bitbucket.ca

    IMHO reading code may not be a good way to start and might net you some really bad habits.

    As for texts there are a few I have found indespensible (and are easily found in libraries.. at least in the ones at my University).

    man pages (they kinda suck if you don't know where to start but once you do they are invaluable)

    Pocket Guide to TCP/IP Socket Programming in C by Michael J. Donahoo and Kenneth L. Calvert (also known as "The Pocket Socket Guide in C"

    UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI, by Richard Stevens Prentice Hall, 1998.

    Both have source code online if you want it.
    Bort are very good books on the topic and have personally read through them.

  2. By r0ach () on

    i found this guide to be really great: http://www.ecst.csuchico.edu/~beej/guide/net/

  3. By gnudutch () on

    Yes, Beej is a wonderful starter page for learning networking I was going to suggest it myself.

  4. By Richy K () richy@pube.org on mailto:richy@pube.org

    lot of _hackers_ recommend to read & read code again.

    but with that, you also have to write & write code again...

    it's one thing to be able to understand a golden nugget of code, but to (re)produce a concept/idea is another.

    -r.

  5. By Hans Insulander () on

    If you have no programming experience, get a good textbook about C-programming for beginners. Can't help you there, sorry. Ask in your bookstore.

    You'll need the "C-book" by Kernighan and Ritchie. I've been a C-programmer for many years and i still use it all the time, so it'll be one of your best investments you'll ever make. Any bookstore with computer books knows which book this is.

    Try to get to know some people to talk to - if you don't already have them. Informal relations with experienced people is the most important thing, IMO.
    For example, see if there are any clubs in your area. If not, usenet and/or mailinglists may be helpful - but make sure your questions are appropriate before posting.

    I think someone else mentioned Stevens' books - they are invaluable and a true goldmine. All the bookstores have them.

    When you read code, don't assume that it's written right. It may be very old, use bad style, use obsoleted interfaces, be buggy etc.

    Last of all, remember to always have fun! You should never feel that you have to write code.

  6. By Andrew () andrew at suspicious dot organization on mailto:andrew at suspicious dot organization

    C is very useful, being one of the most ubiquitous languages around - especially in the Unix world. However, it is probably not the best language to start out with, for several reasons.

    You will be focusing on getting around syntactical issues from the start. This means that you won't be able to focus as much effort on learning real concepts that apply to computer science.

    You will have to worry about garbage collection, making sure your programs don't leak, and bounds checking. These are complex issues, issues that the beginning programmer shouldn't have to worry about until later when he or she has more experience.

    You will have to worry about data types, data conversation between types, precision, and all the issues surrounding a strictly typed language. Again, pretty daunting to someone who should be learning general concepts first.

    Programming is programming, but I would recommend that you start with a language that keeps syntactical issues to a minimum and lets you explore pure computer science ideas without hassle. One very real possibility is Scheme (http://www.schemers.org/). Its syntax is extremely simple, it has garbage collection, and is academically backed (MIT) as a beginning computer science language. MIT even has their text book online free-of-charge: http://www-mitpress.mit.edu/sicp/

    C is a necessity in the software development world. However, trying to jump directly into C is like jumping into the deep end of the pool with little ability to swim yet. Focus on concepts and not implementation issues as a starting programmer, and when you transition to C, you will have much less to worry about.

  7. By Dennis Schwarz () denniss@stickupkids.org on mailto:denniss@stickupkids.org

    started reading The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie and using Notes to Accompany The C Programming Language, by Kernighan and Ritchie (``K&R'') by Steve Summit in for parts that i don't quite understand. playing around with simple userland code: echo.c, yes.c, who.c uname.c, etc. reading and understanding the code, then playing with it (ie. wrote a barebones uname implementation.)

  8. By Sean Cody () null@tfh.ca on http://www.bitbucket.ca

    One very important thing to remmeber when learning to use sockets is that they are NOT JUST for TCP/IP networking. There are other protocol families there too. To make my point a bit clearer:
    "In systens before 4.2BSD, pipes were implemented using the filesystem; when sockets were introducted in 4.2BSD, pipes were reimplemented as sockets."
    - Marshall McKusick. The Design and Implementation of the 4.4BSD Operating System.

  9. By Anonymous Coward () on

    You are a beginner to C? Ok, these are books I'm going to recommend, but they cover some fundamental algorithms and patterns you will see throughout the BSD source tree. Read through these, and you won't go far wrong when traversing the source tree. They include lots of examples of "good style" and will help you recognize good style when you see it.

    "The C Programming Language" by Kernighan and Ritchie.
    "Practice of Programming" by Kernighan and Pike
    "Advanced Programming in the Unix Environment" Richard Stevens.
    "Unix Network Programming" by Richard Stevens

    These books are good for beginners, but even as an advanced programmer I still find myself referring to them, years later. For supplementary reading, I highly recommend

    "Programming Pearls" by Bentley

  10. By gumleef () on

    many others have mentioned the c book by K&R, well here is an online version. get it quick, cos chances are its not official...

    http://bsdroxs.myip.org/books/K_R/kandr.html

    a bunch of other

  11. By iie1195 () kschristensen@yahoo.com on http://www.sweetleaf.info

    Here are the books that I've had good experience with, both for myself, as well as friends:

    "Practical C Programming" by Steve Oualline (ISBN: 1565923065)

    "Linux Socket Programming by Example" by Warren W. Gay (ISBN: 0789722410)
    "Pocket Guide to TCP/IP Sockets (C version)" by Michael J. Donahoo, Kenneth L. Calvert (ISBN: 1558606866)

    "The C Programming Language" by Brian W. Kernighan, Dennis M. Ritchie (ISBN: 0131103628"

  12. By Jim () on

    I, too, highly recommend the Stevens' books for network programming. However, while reading them don't take the code snippets as absolute gospel. In the version I have (not the latest), there is at least one bug in his code, and I'm sick of fixing code that runs mktemp() on the unix socket filename AFTER setting it in the address structure!

    Bottom line--when reading any book, don't switch off your brain. If something looks wrong to you, it well might be. Take the time to understand what the author is trying to do, and read manpages to see if it's right. None of this stuff is magic.

    Jim

  13. By bsdphreak () on

    So, I've read K&R's bible to C & I've done some tiny bits & pieces of programming in C. But the biggest issue I have in trying to delve down & write something cool (or at least semi-useful) is that I don't know much about safe coding -- i.e., how to avoid buffer overflows & such. Are there any books or references on learning how to avoid such dangerous coding mistakes, so that I can prevent them from becoming common in my code? Also, I could use some good references/tutorials/books on the C libraries that are available on the free *nix platforms -- I'm guessing the glibc manual is probably as good a starting place as any, but if there's anything a lil' less daunting I'd be quite interested.


    Thx,
    bsdphreak

  14. By Ryan Ware () on

    I think "How to Design Programs" is excellent. It works with Dr. Scheme. Check out http://www.cs.rice.edu/CS/PLT/packages/drscheme/
    There is a dead trees version too from MIT Press.

  15. By Xavier Brouckaert () gav@bugfactory.org on mailto:gav@bugfactory.org

    Yes, Beej's guide is good, but it only works for IPv4.
    If you want to write AF-Independant applications, take a look at http://www.kame.net/newsletter/19980604/

    Rules :
    1) Use sockaddr_storage everywhere
    2) Use getaddrinfo & getnameinfo everywhere

    Unfortunately, I've always got segfaults using getaddrinfo on Linux with glib-2.2.4-19.
    I think it's fixed in USAGI, but I haven't tried.

    -------

    Program received signal SIGSEGV, Segmentation fault.
    0x40107558 in getaddrinfo (name=0xbffffd84 "chimay", service=0x0,hints=0x40166660, pai=0x80483d9) at ../sysdeps/posix/getaddrinfo.c:787

    Hints appreciated...

  16. By David Sinclair () sinclair@gbg.bonet.se on http://www.gbg.bonet.se/sinclair

    Everywhere I read stuff about new programmers and new tools and programs and everything is written in the C language. Why? C++ is the new industry standard almost everywhere. C has done it's part and I think it's time to move onto C++ which is a more powerful language. So my question as a poweruser in both the UNIX and Windows world, why not C++?

Credits

Copyright © - Daniel Hartmeier. All rights reserved. Articles and comments are copyright their respective authors, submission implies license to publish on this web site. Contents of the archive prior to as well as images and HTML templates were copied from the fabulous original deadly.org with Jose's and Jim's kind permission. This journal runs as CGI with httpd(8) on OpenBSD, the source code is BSD licensed. undeadly \Un*dead"ly\, a. Not subject to death; immortal. [Obs.]