Connect to us Using
    Facebook Login
    Site Registration Why to Join

    Get Free Article Updates

Print Preview

C Coding Style and Best Practices

+9 votes

Following are the list of coding styles and best practices which are followed in various organizations. However these standards varies from company to company but I tried to compile few commonly practiced standards which are useful for good readability and long term maintainability of the code.

  • Outside of comments and documentation, never use spaces. Indentation is done using tabs only;

  • Do not use tabs to align text documentation. Although the preferred tab width is 8 characters, changing tab width should not interfere with the layout/alignment of code, only indentation;

  • Maximum text width is 80 columns, with very few exceptions;

  • Do not put multiple statements on a single line:
    if (condition) do_this;

    if (condition)

  • Variables should always be in lower case, like:
    char *tmp_buf;
    int length;
    int i, j;

  • Macros (#defines) for constants should be in upper case, like:
    #define MAX_NUM_ENTRIES 10
    #define WHATEVER 0x220

  • Do not use typedefs to hide structs or enums, unless it's absolutely necessary to create an abstract type. If that's the case add a '_t' suffix to identify it;

  • Never use typedefs just to hide pointers, unless it's a function pointer;

  • CamelCase is discouraged, with one exception:
    In cases where it better integrates with other coding-style, to avoid mixing different cases (for example, if the code is *heavily* tied to some library which uses such notation);

  • When using a (semi) object-oriented paradigm, separate "classes" from methods/attributes using '__', as below:

    foobar__do_something() ["class":foobar, method:do_something()]
    user_adm__change_pass() ["class":user_adm, method:change_pass()]
    bobex__lock ["class":bobex, attribute:lock]
    backend_sysfs__methods [...]

    This must be done carefully, since it may cause unwanted code pollution;

  • All non-static functions should have a prefix to avoid symbol names clashing, as well as new defined types and exported macros, as in the example below:


  • When performing error checking, use goto to help creating a single return point. Do not abuse goto usage though (avoid going up, going too far, too much labels, etc);

  • The most important lesson about coding style is to keep your source consistent. For example, using non-idiomatic (alien) constructions is strongly discouraged;

    About header files and modularization:

    • Source code has to be split into modules (AKA units), which are defined as a collection of implementation files (.c) with an interface exported through a header file (.h);

    • The inclusion (#include) of headers must reflect the dependencies between modules in the implementation. The most important implications of this statement are:

      . implementation files (.c) *must* include *all* headers it directly depends on;
      . implementation files (.c) *must not* include headers it doesn't directly depend on;
      . headers should only include headers (nested headers) when there's a need for a definition or declaration;
      . headers should never include other headers just to create a "single point of inclusion".

    • Local headers (from the same component) are included using "", with an exception for the auto-generated files "config.h" and "system.h", which can be included using <>.

posted Jan 15 by anonymous

  Promote This Article
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

Related Articles

There are three ways in which a constant can be correlated with pointer -
1. constant pointer
2. pointer to a constant
3. constant pointer to a constant

1. Constant Pointers
A constant pointer is a pointer that cannot change the address its holding. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.

Sample Declaration: int * const ptr;

Sample code

int main()
  int num[2] = {1,2};
  int* const ptr = &num[0];

  printf("%d",*ptr); // This will print 1

  *ptr = 5;
  printf("%d",*ptr); // This will print 5

  ptr++; //This is an compilation error 

Note: We must initialize the pointer at the time of declaration as following will give the error.

int* const ptr;
ptr = &num[0];

2. Pointer to a Constant
A pointer through which cannot change the value of variable it points is known as a pointer to constant. These type of pointers can change the address they point to but cannot change the value at that address.

Sample Declaration: const int* ptr;

Sample Code

int main(void)
    int var1 = 0;
    const int* ptr = &var1;
    *ptr = 1; // Invalid operation as ptr is pointer to a constant

3. Constant Pointer to a Constant
This type is a pointer which is mix of first two which means that a pointer can not point to another variable neither change the value of the variable it point to.

Sample Declaration: const int* const ptr;

Sample Code

int main()
  int num[2] = {1,2};
  const int* const ptr = &num[0];

  printf("%d",*ptr); // This will print 1

  *ptr = 5; // This is a compilation error

  ptr++; //This is an compilation error