User Tools

Site Tools


c_language:c_style

Misc

The Linux kernel coding style

Google C++ coding style

NASA C style guide

POSIX style guide?

C and C++ Style Guides – Archived version of a collection of style guides

libevent – Available API to maybe seek inspiration from. Uses callbacks and stuff.

Embedded C Coding Standard – By Michael Barr

Indian Hill C Style Manual

Things to consider

Naming

int variable, descriptive_variable_name;
 
typedef int own_datatypes_t;
 
void function_names(void);
 
void mylib_library_function_with_prefix(void);
 
typedef enum myenum_t
{
  MYLIB_GLOBAL_SCOPE_ENUM,
} myenum_t;
 
const int constant;
 
#define ANY_DEFINE_OR_MACRO

Embracing

void foo(void)
{
}
 
if(a)
{
}
 
switch(a)
{
  case A:
    break;
  case B:
  case C:
    break;
  default:
    break;
}

Conditional statement whitespaces

if(a == b)
 
// Not any of these
if (a == b)
if( a == b )
 
// Compare to function call
strlen (str)
strlen( str )

Whitespaces in declarations

uint32_t *my_func();
uint32_t* my_func();
uint32_t * my_func();
 
uint32_t *my_var;
uint32_t* my_var;
uint32_t * my_var;
uint32_t *my_var, *another_var, third_var;
 
void a_function_with_a_really_long_name_and_argument_list(int that, int makes, int you, int wonder, int where
                                                          int and_how, int to, int break_the, int lines);

Program flow

  • Should functions only have one return statement? How/when/why?
c_language/c_style.txt · Last modified: 2022/09/12 00:30 by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki