Skip to content

C Style Guide

Sandro Marghella edited this page Nov 8, 2024 · 1 revision

C Style Guide

1 Introduction

This document provides a style guide for writing C code, closely aligned with the Team’s C++ style guide.

2 Naming Conventions

2.1 Variables

  • Use lower_case_with_underscores for variable names.
  • Variable names should be descriptive and concise.
  • Example:
int total_count;
float average_value;

2.2 Constants

  • Use UPPER_CASE_WITH_UNDERSCORES for constant names.
  • Example:
const int MAX_BUFFER_SIZE = 1024;
#define PI 3.

2.3 Functions

  • 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);

2.4 Structs and Enums

  • 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;

2.5 Macros

  • Use UPPERCASE_WITH_UNDERSCORES for macro names.
  • Example:
#define MAX_BUFFER_SIZE 1024
#define SQUARE(x) ((x) * (x))

3 Formatting

Consistent indentation and formatting improve code readability.

3.1 Line Length

  • Limit lines to 80 characters. If a line exceeds this limit, consider breaking it into multiple lines.

3.2 Braces

  • Place opening braces on the same line as the control statement.
  • Place closing braces on a new line.
  • Example:
if (condition) {
    // code
} else {
    // code
}

4 Comments

Proper comments make code easier to understand and maintain.

4.1 Block Comments

  • 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;
}

4.2 Inline Comments

  • Use inline comments sparingly and only for complex or non-obvious code.
  • Example:
int total_count = 0; // Initialize the count to zero

5 File Organization

Organize files and code logically to enhance readability and maintainability.

5.1 File Naming

  • Use lower_case_with_underscores for file names.
  • Example:
main.c
    helper_functions.c

5.2 Header Files

  • Use UPPER_CASE_WITH_UNDERSCORES for include guards.
  • Example:
#ifndef HELPER_FUNCTIONS_H
#define HELPER_FUNCTIONS_H
// Function declarations
#endif // HELPER_FUNCTIONS_H

6 Writing Object-Oriented-Like Code in C

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.

6.1 Encapsulation

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;

6.2 Defining Methods

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);
}

6.3 Constructor and Destructor

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);
}

6.4 Best Practices

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