C Coding Style
Table of Content
- Table of Content
- C Features
- Indentation
- Line length
- Comments
- Braces and Spaces
- Preprocessor
- Functions
- Usefull links
C Features
Use ANSI C or C99 without extension (with the exception of __asm__
and __attribute__((packed))
).
When you use ANSI C the only allowed exception are variadic macros (-Wno-variadic-macro
flag with gcc) and long long (-Wno-long-long
).
Indentation
Indents using tabs and tabs are 4 characters.
Line length
Line length MUST be BELOW 80 colummns.
Comments
Comments use /* ... */
not //
Be aware that comments should be used only when it’s necessary, code must always be self documented.
Braces and Spaces
Braces
{
and }
are always, ALWAYS, on their own line.
if (my_func() < 0)
{
(...)
}
Exception are struct
, enum
, and array initialization.
enum ei_machine {
MACH_X86 = 0x03,
(...)
};
static char array[] = {
'a', 'b', 'c'
};
Space
Use space after: if
, switch
, case
, for
, do
, while
.
But not with: sizeof
, typeof
, alignof
, __attribute__
.
Preprocessor
Functions
Return type MUST be on it’s own line int function definition, it’s make function easier to find (eg: ^function_name
). This do not apply to prototype.
When a function take no argument void MUST be used (eg: int my_function(void);
).
int my_function_prototype(void);
int
main(int argc, char *argv[])
{
(...)
return (0);
}