derived from suckless coding style
- always use
-std=c99 -pedantic
- prefer
// ...
style comments - for multiline comments, keep initial
*
and last*
on their own lines.
/*
* hello!
*/
- do not explicitly test against
NULL
or0
- space after
if
,while
,for
,switch
- make use of
for
initializer (c99) - use
()
withsizeof
- do not use space(s) after the opening
(
and closing)
- do not use space with
sizeof()
- do not indent cases
switch (a) {
case 50:
break;
case 20:
break;
default:
break;
- type names:
HelloThereHowAreYouDoing
- everything else:
hello_how_are_you_doing
- always typedef structs/unions
- capitalize type name, see casing
- feel free to typedef types using builtin types if you feel that
they should be abstracted away
- don't use this too much
- max line length: 80 columns
- if
if
is too complex, wrap the entire statement like this:
if (
something == 3 &&
something_else == 40 ||
i_dont_know == 130) {
// hello
}
each of these sections must be separated by 2 newlines.
section 1 ends (newline)
(newline)
section 2 begins
- comment describing file/license (optional)
- any guards, if any
- e.g.
#pragma once
or#ifndef
guards
- e.g.
- system/std headers
#include <s.h>
- doesn't have to be in alphabetical order
- local headers
#include "s.h"
- doesn't have to be in alphabetical order
- macros/typedefs
- if any macros/typedefs have to be defined before includes, do so but add a comment describing why
- type declarations (if any)
- function declarations (if any)
- function definition (code) + main
- only use
goto
in order to clean up/defer actions upon error
void block() {
if (action() == 1) {
goto fail;
}
return;
fail:
cleanup();
}
- do not indent labels
- opening braces are always on the same line
- except when ending block and continuing (
else
,else if
)
- except when ending block and continuing (
void init_engine(int a, int b) {
// ...
if (a == 20) {
} else if (b == 50) {
} else {
// ...
}
}
- keep variable declarations at the top of block
- if needed, initialize them
void block() {
int a = 20;
bool something = false;
// ...
}
- keep return type and name on the same line
void init_engine(int* a, const char* b) {
// ...
}
- if there are too many arguments, wrap like this:
void init_engine(
int* a,
const char* b
) {
// ...
}
*
is next to type name, not variable name
int* int_ptr;
- things not used outside of the translation unit should be static
- applies to functions and global variables
- always use tabs
- set width to your liking in your editor
- use spaces for alignment
- things should line up regardless of tab width