0% found this document useful (0 votes)
4 views

Coding Sytle Conventions

The document outlines coding style conventions for C programming, emphasizing consistency, readability, and maintainability. Key areas covered include indentation and formatting, naming conventions, commenting practices, variable declarations, error handling, function declarations, memory management, preprocessor directives, code structure, and function complexity. Adhering to these conventions helps ensure high-quality code that is easier to understand and maintain.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Coding Sytle Conventions

The document outlines coding style conventions for C programming, emphasizing consistency, readability, and maintainability. Key areas covered include indentation and formatting, naming conventions, commenting practices, variable declarations, error handling, function declarations, memory management, preprocessor directives, code structure, and function complexity. Adhering to these conventions helps ensure high-quality code that is easier to understand and maintain.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

int result = add(a, b); // Adding a and b


4. Variable Declarations
• Placement: Declare variables at the beginning of a block or function.
void exampleFunction() {
int count;
char name[50];
// Code logic here
}
• Initialization: Initialize variables upon declaration where possible.
5. Error Handling
• Return Codes: Use return codes to indicate success or failure.
int function() {
if (error_condition) {
return -1; // Error
}
return 0; // Success
}
6. Function Declarations and Definitions
• Prototypes: Declare functions at the beginning of the file or in a header file.
int multiply(int a, int b);
• Definition: Follow the declaration with the function definition.
int multiply(int a, int b) {
return a * b;
}
7. Memory Management
• Allocation/Deallocation: Always check the result of memory allocation and ensure deallocation to
avoid memory leaks.

char *ptr = malloc(size);


if (ptr == NULL) {
// Handle allocation failure
}
// Use ptr
free(ptr);
8. Preprocessor Directives
• Include Guards: Use include guards in header files.
#ifndef HEADER_FILE_H
#define HEADER_FILE_H

// 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.

You might also like