C Coding Standards
C Coding Standards
com
C Coding Standards
@alokm014
What Are Coding Rules and Guidelines?
www.robopathshala.com
Why Apply Coding Standard?
www.robopathshala.com
General Principles
www.robopathshala.com
What : Line Width
How?
All lines must be limited to 80 characters.
Why?
1080p is still one of the most popular resolutions
for monitors and it just so happens that with most
code editors you can comfortably fit 2 code
windows at 80 characters side by side, and even
have room for a sidebar if you like that sort of
thing.
www.robopathshala.com
What : Indentation
How?
Indent level is 4 spaces.
Why?
Greatly improves readability.
www.robopathshala.com
What : Braces
How?
Braces must surround each code block, even
single line blocks and empty blocks.
Why?
This prevents bugs when near by code is changed
or commented out.
www.robopathshala.com
What : &&, ||
How?
Unless it is a single identifier each operand of
logical AND and logical OR shall be surrounded by
parentheses.
Why?
Do not depend on C operator precedence rules,
those who maintain the code in the future might
miss this.
www.robopathshala.com
What : static
How?
‘static’ should be used to declare all variables and
function that are unused outside of the modules in
which they are declared
Why?
This reduces bugs.
www.robopathshala.com
What : volatile
How?
● It should be used to declare global variables
accessible by interrupt service.
● It should be used to declare pointer to a
memory-mapped I/O peripheral register set.
● It should be used to declare a global variable
accessible by multiple threads.
● ‘volatile’ should be used to declare delay loop
counters.
Why?
This reduces bugs
www.robopathshala.com
What : const
How?
● ‘const’ should be used to declare variables that
should not change after initialization.
● It should be used as an alternative to #define
for numeric constants.
Why?
This reduces bugs
www.robopathshala.com
What : Comment markers
How?
WARNING: Risk in changing block of code.
TODO: Area of code still under construction.
NOTE: Descriptive comment about why.
Why?
Improves code maintainability
www.robopathshala.com
Safe
Risky
i f (len > 0 && i t r < MAX)
{
...do something
}
www.robopathshala.com
Safe
char * x;
char y;
Risky
char * x; char y;
www.robopathshala.com
Safe
i f ( i t r > 9)
{
state = END;
}
Risky
www.robopathshala.com
Safe
i f ( NULL == count)
{
return true;
}
Risky
i f ( count == NULL )
{
return true;
}
www.robopathshala.com
Do
uint8_t num;
num = 9 + 7;
Do Not
uint8_t num;
num = 9+7;
www.robopathshala.com
Do
#ifdef USE_CRC32
# define MUL_SIZE 152
#else
# define MUL_SIZE 254
#endif
Do Not
#ifdef USE_CRC32
# define MUL_SIZE 152
#else
# define MUL_SIZE 254
#endif
www.robopathshala.com
Do
Do Not
www.robopathshala.com
Do
case TRIA:
...do something
break;
default:
...do something
break;
}
}
Do Not
www.robopathshala.com
What : if, while, for, switch, and return
How?
Shall be followed by one space when there is
additional program text on the same line
Why?
Improves code readability
www.robopathshala.com
What : =, +=, *=, /=, %=, &=, |=, ^=, ~=, and !=
How?
Assignment operators shall always be preceded
and followed by one space
Why?
Improves code readability
www.robopathshala.com
What : Function parameters
How?
Each comma separating function parameters shall
always be followed by one space
Why?
Improves code readability
www.robopathshala.com
What : for loop
How?
Each semicolon separating the elements of a for
statement shall always be followed by one space.
Why?
Improves code readability
www.robopathshala.com
What : Statements
How?
● No line should contain more than one
statement.
● Each semicolon shall follow the statement it
terminates without a preceding space.
Why?
Reduces bugs, Improves code readability
www.robopathshala.com
What : Naming
How?
Module names shall consist entirely of lowercase
letters, numbers, and underscores. No spaces.
Why?
Reduces bugs
www.robopathshala.com
What : Variable Naming
www.robopathshala.com
What : Popularly accepted abbreviations
Term Abbreviation
Minimum min
Manager mgr
Maximum max
Mailbox mbox
Initialize init
Input/output io
Handle h
Error err
www.robopathshala.com
What : Popularly accepted abbreviations
Term Abbreviation
global g
current curr
configuration cfg
buffer buf
average avg
millisecond msec
message msg
nanosecond nsec
number num
www.robopathshala.com
What : Popularly accepted abbreviations
Term Abbreviation
transmit tx
receive rx
temperature temp
temporary tmp
synchronize sync
string str
register reg
previous prev
priority prio
www.robopathshala.com
Do You Find It Helpful?
www.robopathshala.com
@robo.pathshala @alokm014