Coding Sytle Conventions
Coding Sytle Conventions
There are many resources online you may consult. For example see these short one.
https://www.cs.swarthmore.edu/~newhall/unixhelp/c_codestyle.html
https://uchicago-cs.github.io/student-resource-guide/style-guide/c.html
https://www.cs.umd.edu/~nelson/classes/resources/cstyleguide/
Disclaimer & Attribution: The following text is mostly AI generated (excpet clarifying text in red and formatting).
It is a fair summary that may use as a guide. A few lines edited out. Coding style conventions in C programming
help maintain consistency, readability, and maintainability of code. Here are some commonly accepted
conventions:
1. Indentation and Formatting
• Indentation: Use 4 spaces per indentation level. Avoid tabs.
• Braces: Use K&R style or Allman style consistently:
o K&R Style: (opening brace on same line. K&R: Kernighan & Ritchie style1)
if (condition) {
statement;
}
o Allman Style: (opening brace on next line)
if (condition)
{
statement;
}
• Line Length: Limit lines to 80-120 characters. Break long lines appropriately.
2. Naming Conventions
• Variables: Use descriptive names, typically in snake_case (e.g., max_value, user_age).
• Constants: Use UPPER_CASE (e.g., MAX_BUFFER_SIZE, PI).
• Functions: Use camelCase or snake_case depending on the project's style guide (e.g.,
calculateSum(), calculate_sum()).
• Types: Use PascalCase for types and structs (e.g., DataPacket, LinkedList).
• Macros: Use UPPER_CASE with underscores (e.g., #define BUFFER_SIZE 1024).
3. Commenting
• Header Comments: Include a comment at the top of files describing the file’s purpose and author.
/*
* File: example.c
* Author: John Doe
* Description: Example of commenting style in C
*/
• Function Comments: Before each function, describe its purpose, parameters, and return value.
/**
* Function: add
* ---------------------
* Adds two integers.
*
* a: First integer.
* b: Second integer.
*
* returns: Sum of a and b.
*/
int add(int a, int b) {
return a + b;
}
1 Allman wrote many of the utilities for BSD Unix, Denis Ritchie created C, and the Unix OS
1
• Inline Comments: Use sparingly and only to clarify complex code sections. Describe the use of a
variable where it is declared, if not obvious.
// Declarations
#endif // HEADER_FILE_H
• Macro Definitions: Place #define statements in all uppercase.
9. Code Structure
• File Organization: Keep related functions and data together in the same file. Separate di`erent
functionalities into di`erent files.
10. Function Complexity
• Single Responsibility: Each function should perform a single, well-defined task. Keep functions short
and focused.
11. Error Handling
• Check return values from functions, especially for system calls and memory allocations.
By adhering to these conventions, you ensure that your C code is readable, maintainable, and less prone to
errors. Consistency is key, so stick to the conventions chosen by your team or project.