C-Style guidelines CS502

Course

C Style Guidelines

The following are guidelines for programming in C. The guidelines must be followed in class to get your submissions accepted for full credit.

Naming Conventions

  • All variables, functions, and structure names use camelCase.
  • Never use an underscore in a name.
  • Variables always start with a lower case first letter.
  • Function names always start with a lower case first letter.
  • Structures always start with an upper case first letter.
  • Globals and constants are always all upper case with no lower case letters.

Examples: //A constant is all capitals #define PI 3. //One word variable name is all lowercase int hats = 7; //Three word variable name had upper case letters to break words char* allTheCats; //Functions follow the rules of variables int countOdd(int* array, int size); //Structures start with capital letters //also all structures are typedef-ed typedef struct PriorityQueue PriorityQueue; struct PriorityQueue{ int* heapData; int capacity; int size; };

Commenting

Use//line comments for commenting out code, not writting comments.
Use/*and*/for comments.

Well written code should need very view comments. Use good function names and good variables names and avoid comments as much as possible. Comments should be written where they are needed to explain confusing code. If you do not write confusing code, then you should not have many comments.

There are a few required peices of documentation your code must have. These are Doxygen Comments.

  1. The program must have a description in main.c
2)Each file must have a description (in main this is in addition to the program
description).
3) Each function needs a comment above the prototype.
4) Each structure needs a comment at the definition.

The Doxygen comments use two * at the start/**instead of/*for regular comments.

Program Documentation

This is placed only in main.c

/**
@mainpage CS 260 - Homework 3
@section Description
Describe how the entire program works. What is it for?
*/
File Documentation

This is placed in every file. Inmain.cit comes after the program description.

/**
@file
@author Mark Boady <mwb33@drexel.edu>
@date July 20, 2022
@section DESCRIPTION
Write a description of what this file contains here.
*/
Function Documentation

Each function must have a documentation block. The@returnand@param values are optional. Only put them in if you have return values or parameters. This examples uses both. You may have as many@paramcommands as needed.

These are always placed with the prototype.

/**
Draw a right triangle using stars.
@param x is the number of lines to draw.
@return 0 for sucess and 1 for failure
*/
int triangle(int x);
Structure Documentation

A structure has a comment above it with a description of the structure. Each value stored in the structure also has a comment. /** A structure to represent a heap (Priority Queue / Min Heap) Data Structure. */ typedef struct Heap Heap; struct Heap{ int* data; /**< A pointer to your array of numbers. */ int maxSize; /**< The maximum size of the heap before it needs to be resized.*/ int currentSize; /**< The current number of items in the array. */ };

Citing Sources

When you want to cite a source, note the begininng and end of the referenced code. You do not need to cite the course website, slides etc. Any material provided by the Professor does not need a citation. /* Start of Cited Code Describe Reference (website, person, ect):? Provide a link for any website:? */ for (int i=0; i< 10; i++){ printf("%d\n",i); } /* End of Cited Code */

Code Blocks

When writting code blocks the starting bracket is on the same line as the keywork/function.

There should be no spaces between keywords and parenthesis.

Examples:
int f(int x){
int total =0;
for (int i=0; i < x; i++){
if (i % 2 == 0){
total = total + 2;
} else {
total = total * 2;
}
}

return total; }

Code Organization

  • All functions must have prototypes.
  • The main program is always inmain.c.
  • Inmain.c: - All prototypes are before the main function. - All implementations are after the main function.
  • Data Structures: - All prototypes, structs, and typedefs are in the a filestuctname.h - All implementations are instructname.c

Other Rules

  • Never usegotocommands.
  • Programs always end with a newline.
  • Alwaystypedefyour data structures.
  • Avoid globals whenever possible. If you use a global, provide a comment explaining why it was needed.
  • Operators should have spaces around them9 + 2instead of9+2.
  • Use -1 for error if a function would not normally return a negative number.

© 2026 This website is copyright. Created by All Shaman team.