Anda di halaman 1dari 12

Guideline Standards for Coding

C
Language
Doc No: DGL 01

Doc Issue No: 01

Control Status: CONTROLLED COPY IF READ FROM QUALITY SERVER

Approved by

Issuing Authority

Name: Shri. Mohanachandran .R


Designation: Management Representative (M.R)

HEAD,
Quality Assurance Department
C-DAC (T)
Vellayambalam, Thiruvananthapuram

For any clarifications/ corrections/ amendments


in this document please contact Issuing Authority

Copyright
No part of this document
may be reproduced in any form
without the prior permission in writing
of issuing authority

CENTRE FOR DEVELOPMENT OF ADVANCED COMPUTING


Thiruvananthapuram

AMENDMENT HISTORY
1. Issue History
Issue
No

Effective
From

08-Dec2011

Reason for Re-issue

Total
Doc.
Pages

Change Report
Number / Date

NA

10

--

*Approved By: Sd/-

Date: 24-11-2011

Name: Mohanachandran R, Head-QA

2. Revision Details of Current Issue (Issue No. 1)


Doc
Rev
No.

Effective
From

Revised/ Page
Total
Brief Description of change
Added Rev.
Doc.
(If change is major, give document change report number/date only)
Page No No.
Pages

Approved by
(Signature)

* Applicable for documents maintained in hardcopy

Doc. Name :

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No: 01

Table of Contents
Sl. No

Page
No
2

CONTENTS

1.
Scope
2.

2
Notations Used

3.

2
File and Folder Structure

4.

2
Program File Names

5.

3
Program File Header

6.

4
#define Statements

7.

4
Variables

8.

5
Structures

9.

6
Function Declaration

10.

7
Function Header

11.

7
Loops

12.

8
Branches

13.

9
Assignment Statements

14.

10
Comment Statements

15.

10
Coding Guidelines

Doc Name:

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 1

Page Revision No: 00

1. SCOPE
This document deals with the Coding Standards to be followed while writing the C code for
various software processes being developed in connection with the Compact TETRA Base
station project. However, it is not mandatory to use this standard, but if not used, the developer
should follow the QA Procedure for this purpose.
2. NOTATIONS USED
UPPER CASE :
lower case:
Title Case:
<space>:

UPPERCASE
lowercase
TitleCase
The space character

3. FILE AND FOLDER STRUCTURE

Each implementation file shall have its corresponding header file wherever required
Each software module shall have a root folder named after that module
All project related files and other common/shared files shall come under the root folder
All implementation files shall come under a single folder named Source
All header files shall come under a single folder named Header
If any custom made library files are used, those library files shall come under a single
folder named Library

Fig: A possible folder hierarchy


4. PROGRAM FILE NAMES
The names of all files created/edited for a particular product shall start with the
ProductMnemonic followed by corresponding ModuleMnemonic and the FilePurpose
each of which separated by underscore (_).
Syntax:
ProductMnemonic_ModuleMnemonic_FilePurpose.ext
ProductMnemonic:
ModuleMnemonic:

Doc Name:

All letters shall be UPPER CASE alphabets


All letters shall be UPPER CASE alphabets

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 2

Page Revision No: 00

FilePurpose:
ext:
Example:

Shall be a single TITLE CASE word. That word should


explicitly show the purpose of that file.
File extension in LOWER CASE (maximum three
characters).
CTBS_DPRB_CPCIMessageHandler

5. PROGRAM HEADER
Each header/implementation file shall start with a header in the following format.
/*######################################################################
#
#
File Name:
#
########################################################################
#
# Project Name
:
#
# Project Code
:
#
# Created
:
#
# Purpose
:
#
# Description
:
#
# Author(s)
:
#
# Version No
:
#
# Revisions
:
#
# Remarks
:
#
# Copyright
######################################################################*/
Project Name:
Project code:
Created:
Purpose:
Description:
Author(s):
Version No:
Revisions:
Remarks:
Doc Name:

Name of the project


Project code
Date of file creation
Purpose of the file
A brief description of the file
Author(s) name
Module Version No.
Any major revisions done stamped with corresponding date
Any additional remark

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 3

Page Revision No: 00

Copyright statement

Copyright:
Note:

All file inclusions shall be done at the beginning of the file.


6. #define STATEMENTS
All #define labels shall be in UPPER CASE separating each word with underscore (_).
Syntax:

#define LABEL
Example:
#define MAX_BUFFER_SIZE

500

7. VARIABLES
All variable names shall start with VariableScope followed by VariableDatatype and
VariablePurpose each of which separated by underscore (_).
Syntax:
VariableScopeVariableDatatype_VariablePurpose
VariableScope:

VariableDatatype:

VariablePurpose:

A single LOWER CASE letter indicating the scope. g for


global and l for local. This can be omitted for structure
field names.
LOWER CASE alphanumeric characters indicating the
variable data type. If the variable is a pointer, it shall be
indicated by adding p at the beginning of the variable
data type.
Shall be a single TITLE CASE word. It shall explicitly
indicate the purpose of that variable. It shall not contain any
characters other than alpha-numerals.

Example:
luint16_ReceiveInterruptCount
gpuint16_ReceiveData

Doc Name:

: A local unsigned int variable of 16 bits


: A global pointer to unsigned int variable of 16 bits

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 4

Page Revision No: 00

Exceptions:
Loop variables, Structure field variables etc.

8. STRUCTURES
All structures shall be type defined using the typedef statement. All structure declarations
shall follow the following format.
Syntax:
Declaration:
typedef struct STRUCT_STRUCTURE_NAME
{
Datatype Datatype_FieldName1;
Datatype Datatype_FieldName2;
..
..
..
} StructureName;
STRUCT_STRUCTURE_NAME:

Datatype_FieldName:

StructureName:

Name of the structure all in UPPER CASE separated with


underscore (_) with a tag STRUCT.
Structure field name along with its data type separated by
an underscore (_). Variable scope identifier is not
significant in the case of structure field name. But if the
variable is a pointer, it shall be indicated by adding p at
the beginning of the data type.
Type defined name of the structure. Shall be the same
STRUCTURE_NAME but written in TITLE CASE.

Instantiation:
StructureName VariableScopeStructureName_StructureVariablePurpose;
VariableScope:
StructureName:

Doc Name:

A single LOWER CASE letter indicating the scope. g for


global and l for local.
Type defined name of the structure. Shall be the same
STRUCTURE_NAME but written in TITLE CASE. If the
variable is a pointer, it shall be indicated by adding p at
the beginning of the structure name.

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 5

Page Revision No: 00

StructureVariablePurpose:

Shall be a single TITLE CASE word. It shall explicitly


indicate the purpose of that structure variable. It shall not
contain any characters other than alpha-numerals.

Example:
Declaration:
typedef struct STRUCT_DATE
{
uchar uchar_Day;
uchar uchar_Month;
uint16 uint16_Year;
}Date;
Instantiation:
Date gDate_RegistrationDate:

A global Date type structure

variable.
9. FUNCTION DECLARATION
All Function names shall start with FunctionReturnDatatype,
FunctionPurpose each of which separated by underscore (_).

followed

by

Syntax:
FunctionReturnDatatype FunctionReturnDatatype_FunctionPurpose
FunctionReturnDatatype:

Return data type of the function. If the return type is a


pointer, it shall be indicated by adding p at the beginning
of function return data type.

FunctionPurpose:

Shall be a single TITLE CASE word. That word should


explicitly specify the purpose of that function. It shall not
contain any characters other than alpha-numerals.

Example:
uint16 uint16_SendMessage (IPAddress lIPAddress_Destination, char
*lpchar_Buffer, int16 lint16_BufferSize)
{
statement1;
statement2;

}
Doc Name:

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 6

Page Revision No: 00

Note:
All abbreviations that are used in File name, Variable name or Function name shall be
in UPPER CASE.
10. FUNCTION HEADER
Each function shall have a header in the following format. The header shall just precede the
function definition.
/*###################################################################
#
#
Function Name:
#
#####################################################################
#
# Arguments
:
#
# Description
:
#
# Return Type :
#
# Remarks
:
#
###################################################################*/
Arguments:
Description:
Return Type:
Remarks:
11.

Arguments of the function


A brief description of the function
Function return type
Function status (complete/incomplete), usage etc

LOOPS
All loops shall have the following format.
Syntax:
for<space>(start;<space>condition;<space>increment)
{
statement(s);
}
while<space>(condition)
{
statement(s); }

Doc Name:

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 7

Page Revision No: 00

Example:
for (i=1; i<10; i++)
{
statement1;
statement2;

}
while (i<10)
{
statement1;
statement2;

}
12. BRANCHES
All branching statements shall have the following format.
Syntax:
if<space>(condition1)
{
statement(s);
}
else if<space>(condition1<space>&&<space>condition2)
{
statement(s);
}
else
{
statement(s);
}
switch<space>(variable)
{
case value1:
statement(s);
case value2:
statement(s); }
Example:
Doc Name:

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 8

Page Revision No: 00

if (i>10)
{
statement1;
statement2;
}
else if (i>10 && i<15)
{
statement3;
statement4;
}
else
{
statement5;
}

switch (i)
{
case HIGH:
statement1;
break;
case LOW:
statement2;
break;
}
13. ASSIGNMENT STATEMENTS
All assignments shall be of the following format;
Syntax:
VariableName<space>=<space>value;
Example:
guint16_ReceiveInterruptCount = 0;

Doc Name:

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 9

Page Revision No: 00

14. COMMENT STATEMENTS


Comments shall always precede the code statement (wherever possible). Comments shall
ensure good legibility for code walkthrough review by a developer and shall give code
revision hints wherever applicable.

Syntax:

/*<space>This is a comment line<space>*/


//<space>This is a comment line

or

Example:
/* Sending the message */
//Sending the message

or

15. CODING GUIDELINES


1. For constant values use LABELS wherever possible.
Example: for array indices, switch values etc.
2. If possible initialize variables at the time of declaration itself.
3. Declare global variables at the top of the file.
4. Specify the function prototype in the corresponding header file.
Use only TAB character (dont use space) for line/word alignment while coding.

Doc Name:

Guideline Standards for Coding C Language

Doc. No. DGL 01

Doc Issue No. 01

Page No: 10

Page Revision No: 00

Anda mungkin juga menyukai