KB01 - Recommended C style and coding rules
General rules
General Rules
Here are listed the most obvious and important general rules. Please check them carefully before you continue with the other chapters.
- Do not use Tab, use Space instead. In the compiler tools, use 4 spaces to replace a tab.
- Use 4 spaces per indent level.
- use 1 space between the keyword and the opening bracket.
if (condition) // OK while (condition) // OK for (condition) // OK do { // OK } while (condition);
if(condition) // Not Recommend while(condition) // Not Recommend for(condition) // Not Recommend do { // Not Recommend } while(condition);
- Do not use space between the function name and the opening bracket.
int a = sum(3, 4); // OK
int b = sum (3, 4); // Not Recommend
- The opening curly bracket is always at the same line as the keyword (for, while, do while, switch, if, ...)
for (i = 0; i < 5; i++) { // OK }
for (i = 0; i < 5; i++){ // Not Recommend } for (i = 0; i < 5; i++) // Not Recommend { }
- Use single space before and after comparison and assignment operators.
a = b + 5; // OK for (i = 0; i < 5; i++) // OK
a=b+5; // Not Recommend a = b+5; // Not Recommend for (i=0;i<5;i++) // Not Recommend
- Use a single space after every comma.
function1(a, 6); // OK
function1(a,6); // Not Recommend
- Include the following and all other related header files in all your program.
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdbool.h>
- Declare functions and variables before using them.
- Meaningful names for variables, constants, and functions. Do not use camelcase;
- Better to increment and decrement with ++ and -- operators.
- Better to use x += 5 instead of x = x + 5
- A string is an array of characters ending with a ‘\0' (also called a NULL character). Don’t ever forget the NULL character
- An array of size n has indices from 0 to n-1. Although C will allow you to access A[n] it is very dangerous
- A character can be represented by an integer (ASCII value) and can be used as such
- The unary operator & produces an address
- The unary operator * dereferences a pointer
- Arguments to functions are always passed by value. But the argument can be an address of just a value
- For efficiency, pointers can be passed to or returned from a function
- Logical false is zero and anything else is true
- You can do things like for(;;) or while(i++) for program efficiency and understanding
- Use /* .. */ instead of //, it makes the code look better and readable
- The last and most important one is always compiling your program before submitting or showing it to someone. Don’t assume that your code is compliable and contains no errors. Try using –std=c99, which is the C99 standard. It is better. (Although, c11 is also on its way but is not a standard at the moment for all machines)
Comments
Variables
- Always declare local variables at the beginning of the block, before the first executable statement.
- Follow the rules to name the variables:
- Make variable names all lowercase for local variables.
- Make the first character uppercase for global variables.
- Make variable names all uppercase for constant variables (The value of the variable does not allow be modified)
- Declare pointer variables with an asterisk (*) aligned to the variable name.
char *ch; // OK int *pre, *next; // OK
Functions
Structures, enumerations, typedefs
Compound statements
- Every compound statement must include an opening and closing curly bracket, even if it includes only 1 nested statement.
- Every compound statement must include a single indent; when nesting statements, include 1 indent size for each nest.
if (cond) { // OK do_a(); } else { do_b(); }
if (cond) // Not Recommend do_a(); else do_b(); if (cond) do_a(); // Not Recommend else do_b();
- In case of if or if-else-if statement, else must be in the same line as the closing bracket of the first statement.
if (cond1) { // OK } else if (cond2) { } else if (cond3) { } else { }
if (cond1) { // Not Recommend } else { } if (cond1) { // Not Recommend } else if (cond2) { }
- In the case of the do-while statement, the while part must be in the same line as the closing bracket of the do part.
do { // OK } while(cond);
do // Not Recommend { } while(); do { // Not Recommend } while(cond);
- Indentation is required for every opening bracket.
if (cond1) { // OK do_a(); } else { do_b(); if (cond2) { do_c(); } do_d(); }
if (cond1) { // Not Recommend do_a(); } else { do_b(); if (cond2) { do_c(); } do_d(); }
- If while, for, do-while are empty (it can be the case in embedded programming), use empty single-line brackets.
// OK, empty loop contains no spaces inside curly brackets while (cond) {}
while (cond1) { } // Not Recommend while (cond2) { // Not Recommend } // curly brackets are missing. // Can lead to compiler warnings or unintentional bugs while (cond3); // Not Recommend
Switch statement
Macros and preprocessor directives
Documentation
Header/source files
- Include the following header files in all your projects.
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdbool.h>
- Always use < and > for C Standard Library include files, eg. #include <stdlib.h>
- Always use " " for custom libraries, eg. #include "my_library.h"