For general advice see: Linux kernel coding style
The following amendments / clarifications are to be considered additionally:
- Kernel-Normal-Form for Function-Blocks Within function blocks the protypes are to be formatted like in KNF Indentions are four spaced:
int
main(int argc, char** argv)
{
return 0;
}
- No C99 Comments I do not like them. They do not stand out and seem "hide" between regular code. They are ugly. Deal with it:
int crazyInt = 0 /* Perfectly stands out */
- Local variables are to be declared AND initialized before statements in their respective block (as in C89) They start their live on entry of the block anyways, so why restricting yourself with scoping rules:
int
myfunction(void)
{
char buf[12] = {0};
struct *statbuf = NULL;
global = "ouf";
call();
return 1;
}
(PSA: Initializing prevents undefined behavior) Also they are to be seperated by one newline from the rest of the control flow.
- In contrast to the Linux kernel code, when expression are of moderate complexity, please spare the variable. I deem it elegant not to litter one's code with various steps of a calculation.
/* DO AVOID THIS */
int *res = ptr++;
*res = 5;
/* Just inline it */
*(ptr++) = 5;