Loading...
Searching...
No Matches
C Style Guide

This document gives general guidelines on generic C style and code formatting within Sofia-SIP.

The guidelines include identifier naming conventions, indenting convention, and tool usage directions.

Please note that C style is always a matter of taste.

# Naming Conventions

Generally, identifiers within each module are prefixed with the name of that module. For instance, the functions within http parser module http have prefix `http_`. Identifiers composed of multiple words have an underscore "_" between the words, the words themselves are in lower case. For instance, http_request_create().

Macros should be in upper case. File names should be in lower case using underscore as delimiter if needed.

Normal typedefs have suffix `_t`, however, function types have suffix `_f`. The enum names also sometimes have `_e`, struct names have `_s` and union names `_u`.

It is recommended that type itself is typedef'ed, not a pointer to the type. It should be clear from variable declaration if the variable is a pointer or not.

typedef struct foo_s foo_t;
typedef int f_fun(foo_t *f, char const *s);

Struct and union members should have common prefix. For instance,

struct foo_s {
int f_len;
char *f_name;
fun_f *f_fun;
};

This prefix makes it easier to find where members are used.

# Indenting and Formatting code

Indentation in Sofia-SIP C code generally follows the K&R style with indent of 2 characters (so you can use the default "GNU" c-style in Emacs). The maximum line length should be 80 characters.

For example,

void kluge(int foo)
{
if (foo) {
bar();
}
else {
switch (baz()) {
case a:
eeny();
break;
case b:
meeny();
break;
default:
moe();
break;
}
}
}

The default indentation can be achieved with GNU indent with options

-nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci2 -cli0 -cp33 -cs
-d0 -di1 -nfc1 -nfca -hnl -i2 -ip0 -l79 -lp -npcs -nprs -npsl -saf -sai
-saw -nsc -nsob -nss

Loops without condition use `for` ``(;;) instead of `while` ``(1).

for (;;) {
foo();
if (bar())
break;
baz();
}

There should be whitespace on both sides of infix operators, except `.` or `->`, which require no space, or `,` (comma) which requires space only after). There should be whitespace between a keyword and parenthesis following it, but no whitespace between an identifier and parenthesis following it. E.g.,

while (i++ < n)
baz();
for (;;) {
x->x_foo();
if (bar())
break;
z.z_baz++;
}
return (13 * i);

Sofia-SIP 1.12.11devel - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.