C Coding Guidelines

This article will show you about the appropriate coding style. Every programmer needs to follow these guidelines to increase the readability and understanding of code. It also reduces the cost of the software and complexity of code.

If code is written with proper guidelines, It starts saving money from the day one as it will be easy to review by another higher authority person and it will be good reference code for other team members. Maintaining such legacy code will be cheaper in future bug fixes and feature addition.

Formatting

This part of the article will show you about the format of your source code for the betterment.

  1. To split an expression into multiple lines, split it before an operator.

if(variable1 && variable2 < variable3

&&variable4)

2) We should use spaces before the open parentheses and after the commas to make the program readable.

If (X> max (y, z))

3) There shouldn’t be the same level of indentation for two operators of different precedence.

if(A==D

||B>C) // this is not the right way

Instead of it write as mentioned below

if(A==D || (B>C)) // use extra parenthesis so that indentation shows nesting.

4) It is always better to use extra parenthesis for nesting.

Commenting

1) As we all know English is a language which is used for international communication. It is good to use English language for commenting so the purpose of comments can be easily understood by all.

2) A comment should not be more than one line.

3) We should always write the purpose of the program in one line at the top of the program(or above the ‘main’) in the form of comment.

4) Always write the purpose of the source file in the form of comment.

5)There should be a comment for each function which describes the following things

  1. Types of arguments.

  2. The purpose of that function.

  3. The significance of return value.

6) Put at least two spaces after the end of a sentence in comments.

7) There should be a comment for each variable which describes the purpose of that variable.

8) Use comments for nested conditions.

Syntax

1) Don’t put an external declaration inside the function.

2) Declare different variables for different purposes i.e. never use the same variable over and over again for different values in one function.

3) Don’t declare multiple variables of the same datatype in one declaration.

for ex.

int count,

button;

Don’t do this 👆🏻

Instead of it write like this:-

int count, button;

Or

int count;

int button;

4) For nested if statements always use braces for proper indentation.

5) If there is a if statement nested in else then write like this:

If()

…..

Else if ()

…..

6) Avoid assignments inside if conditions

Example:-

If( a= size of(b) == NULL)

Instead of this write like this:-

a = size of (b)

If(a==NULL)

Naming

This will show you about name of the variables and files.

1) Local variable names can be shorter because they are used only within one context.

2) Use underscores to separate the words in a name.

Ex. EmbedkariCodingGuidlines.

Instead of this write like this:-

Embedkari_Coding_Guidelines.

3) To define constant integer use enum rather than using #define.

4) Filename shouldn’t be longer than 14 characters.

5) Name of the variable should be meaningful and it should be easy to understand.

Author  Shivani Mundra

Shivani is pursuing BTech. In an electrical engineering from institute of technology Nirma University Ahmedabad. She is currently in 3rd year. She is a gold medalist at Mitsubishi electric cup 2020. Her field of interest is embedded systems, mesh networking etc. She is also a technical head of the departmental club(EESA) at Nirma University.

Thanks for reading till end. If you liked it, Please share in your network.

Have a look at Embedkari Low Cost Products 

You may  find interesting topics at HowTo

Also subscribe  Embedkari for other interesting topics.

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.