Thread: How do you structure your C programs.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    66

    How do you structure your C programs.

    Hi. I`ve written some smaller projects in C, like programs which were listening on a specified port, and parsed coordinates coming from a gps device, or mini chat servers, so I know how to do some things in C, but I don`t have experience in it. Especially I don`t have experience in writing easily extensible code in C.

    At work for years I used languages like Java, Actionscript 3, little objective C, and because these languages have an object oriented nature they made me think my projects in an OO way, like there is Class B which extends Class A, and overrides the methods where the implementation is different, etc. Because C doesn`t have classes I`ve looked around for some techniques to simulate them, like structures which have function pointers, or functions which call other functions instead of overrides, but the resulting code was messy, hard to understand (even by me after a time).

    I tried to look at projects like the apache web server, php, svn or git, to steal some ideas and techniques, but these are large projects and just by reading the source code, I wasn`t able to see, how they are structured overall.

    I would like to know how do you structure your projects, what techniques do you use to keep the code understandable and easy to maintain and extend without generating redundant code? I`m not interested in code samples necessarily, just the ideas behind them. Or if you know any good books about writing quality c code, please give me some links.

    Kernighan and Ritchie did a wonderful job creating this language, I like it`s simplicity and I would like to know not only how to write an if statement or declare a pointer, but also how to organize larger projects to be easy to maintain and understand.

    Thank you

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you like object-oriented languages so much, why not use C++?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    66
    I didn`t say I like object oriented languages, I only said that I used some of them at work. In fact I don`t care if it`s oo, procedural, structural or anything else, as long as the code is clean, and easy to maintain.

    I would like to know C better (not C++), this is why I asked for your oppinions about how do you build up your c projects.
    Last edited by raczzoli; 10-13-2012 at 04:30 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    My perspective might be a little bit skewed, as the systems I work on are larger than what a lot of people here do. My notion of small, for example, is anything smaller than a team of 5 and 10 man-years of development effort.

    However, I think you're asking the wrong question. Apart from some political realities (it would be pointless choosing an OO design if I'm allocated a team of hard nosed C programmers who prefer to do things procedurally), I look at things like system architecture (how significant parts of the system interact), data (what types of data are passed between parts of the system), and information flow (algorithms, control, etc). I (I'm using the royal "I" here, as this is often a team process) don't think in terms of building a "C project", a "C++ project", a "Java project", a "Fortran project", a "multi-language project", or anything else.

    After the hard yards with architecture, data, and information flow, I reach a preliminary design. From that, I might make an initial call on whether a design is OO, procedural, functional, a mix of those, or anything else. And then I choose the language(s). From that point, I start refining the preliminary design to produce a detailed design - which will obviously have some relationship to the preliminary design, but does look more at implementation trade-offs, including those affected by the choice of language(s). Then I take a breather, arrange review of the design (ideally with some independent folks, supported by members of the team). If the detailed design is found to be too shaky - and political constraints don't stop me - I'll put it to one side, and redo the process.

    Once a design is accepted, there will be a bunch of information about how the software (and other system components) is to be structured. One of the side effects of that will be an initial basis for deciding how the source code will be organised (what data type and function specifications get grouped together, etc). That is when the coding will start. As the code is developed, the designs will also be revisited ......
    Last edited by grumpy; 10-13-2012 at 10:55 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  2. Replies: 1
    Last Post: 11-09-2009, 07:03 AM
  3. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  4. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  5. Programs opening programs
    By LinuxPLC in forum C Programming
    Replies: 1
    Last Post: 11-21-2002, 12:50 PM