User loginNavigationWho's onlineThere are currently 0 users and 0 guests online.
Search |
FormattingLine lengthEvery single code line should not exceed the 80 characters limit, since the ISO/ANSI screen size is 80x24 characters wide and exceeding that can cause readability problems to users of such terminals. IndentationIndentation of code blocks is always 8 characters long and is done by tabs. The code that has to be indented is the one between braces (
for (i = 1; i < N; i++)
v[i] = v[i - 1] * 2;
if (v[N - 1] > 1000)
printf("Big number!\n");
else
{
printf("Little number!\n");
v[N - 1] += 1000;
}
As you can see, we put open braces on a line by themselves, indented by two white spaces. This rule applies for every construct apart from function bodies, where we put no indentation on the braces. It goes like this:
NACORE_PUBLIC_FUNC(int,
my_func, (int a, int b))
{
...
}
For
switch (c)
{
case 'A':
i--;
break;
case 'B':
i++;
break;
case 'C':
i *= 2;
break;
default:
i /= 2;
break;
}
While this is our style for
static int
div_positive(int a, int b)
{
if (b <= 0)
goto error;
if (a < 0)
goto error;
return a / b;
error:
printf("ERROR!\n");
return -1;
}
And for preprocessor directives #ifdef HAS_FOO # include <foo.h> # ifdef HAS_BAR # define MYLIB_BAR_DEBUG # incluude <bar.h> # endif /* HAS_BAR */ #else /* HAS_FOO */ # include <none.h> #endif /* HAS_FOO */ Vertical and horizontal spacesIn the previous examples you could already have noticed some other conventions being used for spaces. Let's summarize them:
Furthermore, sometimes it is impossible to respect the 80 characters limit. In the case of parameters, they could be split like this:
my_func(this_function, takes, a, very, long_list, of, params,
so, we, split, them, this_way);
For strings:
printf("This printf() call prints 3 values on the screen, "
"a: %d, b: %d, c: %d\n", a, b, c);
For comments: /* This is an incredibly long and clueless sentece to show how * lines should be split in comments. */ And, in the end, for expressions:
voltage = (sigma / (2.0 * EPSILON_0))
* (sqrt(pow(z, 2) + pow(R, 2)) - z);
Notice that the line has to be split before the operator, not after. Then you have to respect also bracket nesting, like in the following case:
voltage = (lambda / (4.0 * PI * EPSILON_0))
* log((length
+ sqrt(pow(length, 2)
+ pow(distance, 2)))
/ distance);
|