C Formatting Requirements

  1. Include a comment block at the top of EVERY file, which includes the filename, author, course with section number, assignment, and a description of the program or what the file contains. Note the SPACING and ORDER.

    /*
      Filename   : Primes.c
      Author     : Gary M. Zoppetti
      Course     : CSCI 380-01
      Assignment : Program 2, part A (be specific)
      Description: Calculate prime numbers between 2 and N (use your own words)...
    */
        
  2. Use BLANK LINES between blocks of code to enhance readability. Think of the code as if you are writing paragraphs in a paper. When your code shifts to a “new thought,” place a blank line.

  3. Place return types on a line by themselves for function (method) declarations and definitions. Include parameter names in both.

    // Declaration (prototype)
    float
    calcInterest (float balance);
    
    ...
    
    // Definition
    float
    calcInterest (float balance)
    {
      // body
    }
        
  4. Place curly braces on a line by themselves.

    if (isSorted)
    {
      printf ("naturally");
    }
    else
    {
      printf ("oops -- user error?");
    }
        
  5. Include a space before left parentheses. If the left parenthesis is starting a parameter or argument list the space is optional (I still include it). Flank binary operators with spaces.

    interest = calcInterest (balance);
    // OR
    interest = calcInterest(balance);
    
    int a = 3 * (4 + 5);
    // NOT int a=3*(4+5)
        
  6. Use between 2 and 4 spaces for each level of indentation. Be CONSISTENT.

  7. Use Java naming conventions (lowerCamelCase and UpperCamelCase) and choose meaningful, descriptive names. If a value with units is represented, include the units in the name. Constants should be in all CAPS, with underscores used to separate words.

    Avoid using SINGLE LETTER names except when there is no easy way to create a variable name that is descriptive. For example, "s" is OK for the name of a String if you are referring to any generic string and not something specific such as a word or name (in which cases "word" or "name" would be preferable).

    float rotationAngleRadians;
    const unsigned NUM_SORT_VALUES = 10'000;
    
    // Start method names with a verb to convey action
    void
    convertToLower (char* s);
    
    // Use nouns for struct names
    struct Dictionary;
          
  8. Use pre-increment and pre-decrement instead of the postfix versions.

    ++i;
    --j;
      
  9. Define "main" BEFORE any other functions. This will require you use prototypes.

  10. Don't write useless comments.

    // Loop while "cond" is false
    while (! cond)
    {
      ...
    } // end while
    
    // Make "i" point to the beginning element
    i = begin (v);
      
  11. Write comments ABOVE the line you are commenting.

    // Comment here
    sort (v);
    
    sort (v); // NOT here