This repository follows the K&R (Kernighan and Ritchie) and Plan 9 coding style. These are well-known styles widely used in the C programming community. The goal is to maintain consistency and readability in the codebase.
Although some minor deviations are acceptable, try to stick to the same style as much as possible. Below are the basic points and examples to follow when writing code.
Place braces on the same line as control statements (if
, else
, while
, for
):
if (x == y) {
s1;
s2;
} else {
s3;
s4;
}
Place braces on the same line with following a space after function definitions:
type functionname(args) {
if (a == b) {
s1;
s2;
}
else {
s3;
s4;
}
}
No braces around single statement blocks, and use new line:
if (a == b)
s1;
Indent with TABs. Do not use spaces for indention.
Function return type should be placed on a same line:
int functionname(int a, int b) {
return (a - b);
}
functionname(3, 5);
- Do not use more then 4 parameters in functions.
- Do not make functions longer than 25 lines.
- Return values must be in parentheses.
- Function calls should be formatted without spaces.
- Provide checks using assert messages in functions.
Group all variable declarations together at the start of the function or block if possible.
Do not assign variable values on the line where you define them.
int functionname(args) {
int a, b;
a = 3;
b = 5;
return a * b;
}
- Use
int
for integers,float
for floating-point numbers andchar
for characters. - Use descriptive types for fixed-width integers such as
uint8_t
,int64_t
.
Avoid magic numbers in your code. Instead, use #define
constants or enum values to represent any numerical values that are used in multiple places.
#define MAX_SIZE 100
for (i = 0; i < MAX_SIZE; i++) {
// Code
}
Declare pointers with the asterisk (*) next to the type, not next to the variable name.
int *ptr;
Declare struct with s_
prefix and use typedef in external line.
struct s_my_struct {...}
typedef struct s_my_struct my_struct;
In the same way, define enums with e_
prefix.
enum e_my_enum {...}
typedef enum e_my_enum my_enum;
- Use snake_case
- Use descriptive variable names: Avoid one-letter variable names except for loop counters.
- Do not use Hungarian notations, it's just ugly.
- Do not use platform-specific types like BYTE, DWORD unless writing platform-specific code (e.g., Win32).
Do not copy-paste code from other sources (e.g., IDA). Always ensure the code is formatted correctly and adheres to the coding standards.
- Keep code clean and well-structured: Make sure the code is easy to read and maintain. Avoid long functions or complicated structures when simpler solutions are available.
- Group related functions together: In general, functions that serve similar purposes should be located near each other.