Skip to content
Pedro A. Hortas edited this page Apr 16, 2015 · 17 revisions

Coding Style

Indentation

  • Do not indent more than 4 times.
  • Refer to the following subsections to see the indentation style used on the project.

Functions

/* A function with a few arguments */
int func_few_args(int arg1, char *arg2, const char *arg3) {
        ...

        /* Check for errors */
        if (error) {
                return -1;
        }

        /* All good */
        return 0;
}

/* A function with lots of arguments */
int func_many_args(
        int arg1,
        int arg2,
        char *arg3,
        const char *arg4,
        long arg5,
        unsigned long long arg6)
{
        ...

        /* Check for errors */
        if (error) {
                return -1;
        }

        /* All good */
        return 0;
}

Switch / Case

switch (opt) {
        case ONE: {
                ...
        }; break;
        case TWO: {
                ...
        }; break;
        default: {
                ...
        }; break;
 }

If / Else

if (cond1) {
        ...
} else if (cond2) {
        ...
} else {
        ...
}

For / While

while (cond) {
        ...
}

for (i = 0; cond; i ++) {
        ...
}

Increments / Decrements

i ++;
-- i;

Assignments

var = value;

Compare

a < b
b > a
a <= b
b >= a
a == b
b != a

Arithmetic

a + b
b += a * c
d = (a + b) * c

Portability

Endianess

  • Always use stdint.h types (uint16_t, uint32_t, etc) on variables that can possibly be transmitted to other systems.
  • Always use htons(3), htonl(3), ntohs(3) and ntohl(3) on uint16_t and uint32_t type variables that are intended to be transmitted to other systems.

Standards

C99 / C11

Grant that your code conforms to C99 and/or C11.

POSIX

  • Grant that your code conforms to, at least, POSIX.1-2001. If, for some good reason, an optimization non-POSIX compliant is used, that portion of code must be optional (and determined in compile time whether it'll be used or not).

Clone this wiki locally