-
Notifications
You must be signed in to change notification settings - Fork 2
C Style Guide
This document provides a style guide for writing C code, closely aligned with the Team’s C++ style guide.
- Use lower_case_with_underscores for variable names.
- Variable names should be descriptive and concise.
- Example:
int total_count;
float average_value;
- Use UPPER_CASE_WITH_UNDERSCORES for constant names.
- Example:
const int MAX_BUFFER_SIZE = 1024;
#define PI 3.
- Use lower_case_with_underscores for function names.
- Function names should be verbs or verb phrases.
- Example:
void initialize_buffer ();
int calculate_sum(int a, int b);
- UseTitleCase for struct and enum names.
- Use lower_case_with_under_scores for struct members.
- Example:
typedef struct {
int member_id;
float member_value;
} StructName;
typedef enum {
ENUM_VALUE_ONE,
ENUM_VALUE_TWO
} EnumName;
- Use UPPERCASE_WITH_UNDERSCORES for macro names.
- Example:
#define MAX_BUFFER_SIZE 1024
#define SQUARE(x) ((x) * (x))
Consistent indentation and formatting improve code readability.
- Limit lines to 80 characters. If a line exceeds this limit, consider breaking it into multiple lines.
- Place opening braces on the same line as the control statement.
- Place closing braces on a new line.
- Example:
if (condition) {
// code
} else {
// code
}
Proper comments make code easier to understand and maintain.
- Use block comments for descriptions of code blocks or file-level comments.
- Example:
/*
* This function calculates the sum of two integers.
*/
int calculate_sum(int a, int b) {
return a + b;
}
- Use inline comments sparingly and only for complex or non-obvious code.
- Example:
int total_count = 0; // Initialize the count to zero
Organize files and code logically to enhance readability and maintainability.
- Use lower_case_with_underscores for file names.
- Example:
main.c
helper_functions.c
- Use UPPER_CASE_WITH_UNDERSCORES for include guards.
- Example:
#ifndef HELPER_FUNCTIONS_H
#define HELPER_FUNCTIONS_H
// Function declarations
#endif // HELPER_FUNCTIONS_H
While C is not an object-oriented language, it is possible to implement object-oriented programming (OOP) principles using structures and function pointers. This section outlines best practices for writing object-oriented-like code in C.
Encapsulation involves bundling the data and the methods that operate on the data within a single unit, typically using structures.
- Usestructsto represent objects.
- Define function pointers within structs for methods.
- Example:
typedef struct {
int x;
int y;
void (* set_coordinates)(struct Point* self, int x, int y);
void (* print_coordinates)(struct Point* self);
} Point;
Methods are defined as functions that take a pointer to the struct as their first argument.
- Example:
void set_coordinates(Point* self , int x, int y) {
self ->x = x;
self ->y = y;
}
void print_coordinates(Point* self) {
printf("Point (%d, %d)\n", self ->x, self ->y);
}
Implement constructors and destructors to initialize and clean up objects.
- Example:
Point* create_point () {
Point* point = (Point*) malloc(sizeof(Point));
point ->set_coordinates = set_coordinates;
point ->print_coordinates = print_coordinates;
return point;
}
void destroy_point(Point* point) {
free(point);
}
- Use clear and consistent naming conventions for methods and struct members.
- Use constructor and destructor functions to manage the lifecycle of objects.
- Implement function pointers within structs to achieve polymorphism.
- Document the design and usage of object-oriented-like constructs clearly.