Docsity

C Programming Style Guide - Lecture Slides | COP 3502, Lab Reports of Computer Science

Toggle sidebar
bg1
Header
For this course, each program should contain the following header information
Modularization
Separate your program into independent, self-complete modules. We’ll discuss this in
more detail later.
Consistency
Make your style as consistent as possible. The more consistent your style, the easier it
will be to read and understand your program (not just others, but yourself as well!)
Consistency will lead to fewer mistakes. The more consistent your style, the more
automatic it will become for you and you’ll spend less time thinking about program style
and more about the problem at hand.
Indentation
Make the indentation of your code reflect the structure of the program. For example,
if (count == 0) printf (“No data.\n”);
fails to match the structure of the program. It is much better to write this as:
if (count == 0)
printf(“No data\n”);
Here’s another bad example:
if (count != 0)
printf(“%d\n”, count);
average = total/count;
This last example is much more clear if it is indented as follows:
C Style Guide - 1
COP 3502 – Summer 2002 – C Programming Style Guide
COP 3502C – Summer 2002
Lab Number:
Assignment Number:
Name:
Student ID:
Date:
pf3
pf4
pf5

Partial preview of the text

Download C Programming Style Guide - Lecture Slides | COP 3502 and more Lab Reports Computer Science in PDF only on Docsity!

Header

For this course, each program should contain the following header information

Modularization

Separate your program into independent, self-complete modules. We’ll discuss this in more detail later.

Consistency

Make your style as consistent as possible. The more consistent your style, the easier it will be to read and understand your program (not just others, but yourself as well!) Consistency will lead to fewer mistakes. The more consistent your style, the more automatic it will become for you and you’ll spend less time thinking about program style and more about the problem at hand.

Indentation

Make the indentation of your code reflect the structure of the program. For example, if (count == 0) printf (“No data.\n”); fails to match the structure of the program. It is much better to write this as: if (count == 0) printf(“No data\n”); Here’s another bad example: if (count != 0) printf(“%d\n”, count); average = total/count; This last example is much more clear if it is indented as follows:

COP 3502 – Summer 2002 – C Programming Style Guide

COP 3502C – Summer 2002 Lab Number: Assignment Number: Name: Student ID: Date:

if (count != 0) printf(%d\n”, count); average = total/count;

Vertical Flow

The natural flow of reading is vertically down the page. Things that get in the way of this or detract from this flow will make a program harder to read. We’ll see this more later in the semester as well.

Declarations

Make each variable declaration unique. Don’t put more than one variable into a single declaration. Also line up the names in the variable declarations, it will make finding a variable much easier. GOOD: int i, j, k; float: a, b; BAD: int i, j, k; float a, b;

Compound Statements

The braces which define the begin and end of a compound statement or a block of statements should be vertically aligned. There are two acceptable styles and you should adopt one or the other. Style 1: if ( … ) while ( … ) { { … … } } else { … } Style 2: if ( … ) { while ( … ) {

  • COP 3502 – Summer 2002
  • Lab Section:
  • Assignment Number:
  • Date:
  • NAME: ****************************************************************/ /****************************************************************
  • Description of the program

****************************************************************/ /* List all of the header files used here. #include <stdio.h> #include <stdlib.h> #include <time.h> /* List function prototypes here. / void func1 (int x, int y); int func2( ); / Describe the purpose of func1 / void func1 (int x, int y) { . . . } / Describe the purpose of func2 / int func2 ( ) { . . . } int main (void) { / indent the declarations and the body of the functions by at least three spaces. / int x; / if necessary describe the variable’s purpose / int y; / use short but meaningful variable names. */

/* leave a blank line before and after a loop. /* denote the end of a loop with a comment / return 0; } / end main */