Structure Of C Program


When creating your programs, here are a few guidelines for getting a program that can
be easily read and debugged by you (when you come back to it later) and others (who
may have to change your program later).

  • A well-structured program should indent (i.e. move to the right by pressing
    the tab key once) all instructions inside each block (i.e. following an opening
    curly brace – {and closing curly brace}). Therefore, a block inside another
    block will be indented twice etc. This helps to show which instructions will be
    executed, and also helps to line up opening and closing curly braces.
  • It is often helpful to separate stages of a program by putting an extra blank
    line (and maybe a comment explaining what the next stage does) between
    each stage.
  • Another useful technique that helps the readability inside each instruction is to
    put spaces after commas when listing parameters (i.e. data) to be given to a
    function. This helps identify where each parameter starts and makes the
    program easier to read. Spaces before and after brackets, assignments (=)
    and operators (+, -, *, /, <, >, ==, != etc.) can also help make things more
    readable – but this is up to you.
  • Start a program with a /* comment */ that explains the purpose of the
    program, and who wrote it. This is useful in case you ever have to work on
    somebody else's program, and you need to ask them for advice on how it
    works.
  • If you create a new function, put a comment before it showing what data goes
    in, and what comes out, and the purpose of the function.
  • If a part of your code is quite difficult to follow, or you wish to explain why
    you have written it the way you have, or even if you wish to say that it could
    be improved (and how) but you haven't the time to do it better yet, then use
    a comment.

Custom Search

%d bloggers like this: