C style

Names

Struct/Union

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
typedef struct my_struct_t my_struct_t;
struct my_struct_t {
    int some_var;
};

void some_fct(my_struct_t my_struct)
{
    my_struct_t my_struct2;
    my_struct2.some_var = 3;
}

Enum

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
typedef enum my_enum_t my_enum_t;
enum my_enum_t {
    MY_ENUM_CASE_1,
    MY_ENUM_CASE_2,
};

void some_fct(my_enum_t my_enum)
{
    my_enum_t my_enum = MY_ENUM_CASE_1;
}

Typedef

1
typedef u64 physical_address_t

Forward declaration

typedef struct my_struct_t my_struct_t instead of struct my_struct_t

Braces

1
2
3
4
5
6
7
8
9
int fct()
{
    if (condition) {
        do_something();
    }
    else {
        do_thing();
    }
}

Switch

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
switch (type) {
case case_1:
    do_stuff();
    break;
case case_2:
    do_other_stuff();
    break;
default:
    do_some_stuff();
    break;
}

Const

Headers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/*
 * Copyright (c) 2024, Keven Villeneuve.
 *
 * SPDX-License-Identifier: MIT License
 */

#pragma once

#include "my_file.h"

#include <stdio> // std first
#include "some_other.h"

[...]

// one empty line at end

Increment

Always use i++ and not ++i unless absolutely necessary. Let the compiler optimize.

Long int

Use LP64 data model (u64 = unsigned long int = unsigned long long int).

1
printf("%lu", (u64)1234);

Default int type

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdbool.h> // bool
#include <stddef.h> // NULL
#include <stdint.h> // uint_t

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

References

This is very controversial, no one seems to agree...