C - Elements of Style

Peter Kitson

ISBN : 1558512918

Order a printed copy of this book from Amazon .

Cover Design - C - Elements of Style

For your free electronic copy of this book please verify the numbers below. 

(We need to do this to make sure you're a person and not a malicious script)



Sample Chapter From C - Elements of Style
     Copyright © Steve Oualline

Program Aesthetics

A properly designed program is easy to read and understand.

Part of what makes books readable is good paragraphing. Books are broken up into paragraphs and sentences. A sentence forms one complete thought, and multiple sentences on a single subject form a paragraph.

Code paragraphs

Similarly, a C program consists of statements, and multiple statements on the same subject form a conceptual block. Since "conceptual block" is not a recognized technical term, you may just as well call them paragraphs. In this book, paragraphs are separated from each other by a blank line. You can separate paragraphs in C in the same way.

Omitting paragraphs in code creates ugly, hard-to-read programs. If you've ever tried reading a paper without paragraphing, you realize how easy it is to get lost. Paragraph-less programming tends to cause the program to get lost:

/* Poor programming style */
void display(void)
    int start, finish; /* Start, End of display range */
    char line[80]: /* Input line for events */
    printf("Event numbers? ");
    start = -1;
    finish = -1;
    fgets(line, sizeof(line), stdin);
    sscanf(line,"%d %d", &start, &finish);
    if (start == -1)
    if (!valid(finish))
        finish = start;
    if (valid(start))
        display2(start, finish);