0% found this document useful (0 votes)
4 views

10. C Coding Guidelines

Uploaded by

yoyoshyam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

10. C Coding Guidelines

Uploaded by

yoyoshyam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Slide 1

C Coding Guidelines
Slide 2

Coding Standards

 Programming standards is to support the development of


applications that are consistent and well written.

 Standards and guidelines help developers to create a code base


with a uniform presentation, which leads to code that is easy to
understand, easy for other developers to use and easy to maintain.

 Standards and guidelines also help developers to avoid the common


pitfalls of C that leads to code that is robust, reliable and portable.

2
Slide 3

Why Coding Standards?

Coding Standards for C are essential and should be adopted to achieve


the following goals.
 Facilitate joint development
 Avoid common pitfalls
 Maintainability
 Reliability
 Portability
 Understandability

3
Slide 4

Data Definition Standards

 Constants
 Use upper case
 int SECONDS = 60;
 Macros
 Use upper case
 #define PI 3.14
 Functions
 Use lower case with _ as separator
 double get_area(int);

4
Slide 5

Data Definition Standards

 Structures
 T followed by mixed case
 struct TmyStruct{
int age;};
 Enumerations
 E followed by mixed case
 emun EmyEnum{
eMonday,
eTuesday,
eWednesday};

5
Slide 6

Variables Definition

Variable Variable Name Variable


Variable Name = Prefix in Hungarian Suffix
Notation

Prefix Datatype Suffix Variable Type


b Boolean _g Global or External
c Character _s Static
i Integer _r register
li Long

f Flaot

d Double

Learn more about Hungarian Notation here,

http://web.mst.edu/~cpp/common/hungarian.html
Slide 7

Variables Definition

Example: An unsigned int variable which will hold the value of


number of students available in the class can be declared as:
unsigned int uiStudents;
 For Global or External variables:
unsigned int uiStudents_g;
 For Static variables:
unsigned int uiStudents_s;
 For Register variables:
unsigned int uiStudents_r;

7
Slide 8

Variables Definition

 Avoid declaring dimensionless or implicitly dimensioned arrays


1. int aiArray[] = { 0,1,2,3,4 } ; /* not
recommended */
2. char acString[] ; /* not recommended */
3. int aiArryay[5] = { 0,1,2,3,4 } ; /*
recommended */
4. char acString[80] ; /* recommended */

8
Slide 9

General Standards – Defining Data

 Do not use any identifiers which are reserved for compilers, libraries
and headers.
 To represent the null character do not use “”, use „\0‟ or NULL.
 Do not assume that uninitialized variables are set to zero. Always
initialize variables and structures where it is required to initialize
them explicitly.
 Avoid using constants throughout the programs, instead, used
#defines. Also, predefined #defines from header files should be
used. For example: M_PI is defined in <math.h>

9
Slide 10

Activity

Look at the following variable names and list out the data type of each
of the following. Example – ‘uiStudents‟ is a unsigned integer.
 usiOption
 ldStars
 bChoice
 uliBooks
 cInitialLetter
 fArea

10

Answers:
unsigned short
Long double
Boolean
Unsigned long integer
Character
Float

Avtivity Duration: 10 mins


Slide 11

Standards - Files and Layout

 There should be a module specific forward declaration header files


that shows the derivation hierarchy.
 Header files will have extensions .h
 Encapsulate the environment dependent defined types and macros
in the local standard header by using #defines and #ifndef
statements.
 It is recommended that the original author and all major contributors
add one line each along with revision details.
 Functions and variables may have comments describing the
purpose in short.

11
Slide 12

Headers at the beginning of .c/.h files

12
Slide 13

Function Names & Headers

 Each function should be accompanied by appropriate reference


documentation
 Use mixed case letters in naming a function. For example,
Void UpdateList(const List& pList);
 Though the length of the function name is operating system/compiler
dependent but it should be meaningful
 The function header precedes the function definition and have the
following format:

13
Slide 14

Function Header

/*
Name: Function Name

Description: Function Description (Short)


Parameters: <Parameter type><Parameter name><Description>
Parameter type can be : [I], [O] or [IO]
Returns: Return value and description
Globals Updated: Other Source Files this function updates

Tables updated: Tables in database this function updates

Calls: Other functions this function calls

*/

14
Slide 15

Function Definition

As per ANSI function shall be defined in a following format:

<return type>
<function name> (<parameter declaration>)
{
<declarations and statements>
}

15
Slide 16

Example

int
ML_QaGetErrMsg (int iErrorNo, /* Comment */
char *pcErrorMsg /* Comment */
)
{
-----------------------
-----------------------
}

16
Slide 17

Activity

Can you write a program as per the standards which calculates the
area of the circle when user provides the radius of the circle from the
console. Program must have a function which is used to calculate the
area. Also display the area of the circle if radius provided by the user is
assumed to be half and as well as double of itself.

17

Activity Duration: 15 mins


Slide 18

General Standards – Coding Style

 Size of a function can be maximum of 200 lines. Comments are not


included.
 Size of a file can be of maximum 2000 lines. Comments are not
included
 File name should not exceed 14 characters including only one
extension character and not more than 3 characters after extension.
 Comments should only be used to explain the code which is not self
explanatory.
 Always use parenthesis in complex arithmetic statements as a good
practice.

18
Slide 19

Coding Style – Placement of Braces

 Style 1
Control
{ Opening brace in a new line
statement;
}
 Style 2
Control{ Opening brace in a same line
statement;
}

19
Slide 20

Coding Style – Simple Statements

 Put one statement per line.


 Indent statement by four spaces where required.
 Put semicolon one space after the executable statement.
 Make full use of operators. For example:
i++ ; is faster than i = i +1 ;
 Do not use floats for logical checks.
 When possible test for inequality instead of equality. Design
functions to return the values in such a way.

20

Refer to the complete C standard Guidelines from HCL’s OMS Portal.

myHCL  My Learning  –

You might also like