10. C Coding Guidelines
10. C Coding Guidelines
C Coding Guidelines
Slide 2
Coding Standards
2
Slide 3
3
Slide 4
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
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
f Flaot
d Double
http://web.mst.edu/~cpp/common/hungarian.html
Slide 7
Variables Definition
7
Slide 8
Variables Definition
8
Slide 9
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
11
Slide 12
12
Slide 13
13
Slide 14
Function Header
/*
Name: Function Name
*/
14
Slide 15
Function Definition
<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
18
Slide 19
Style 1
Control
{ Opening brace in a new line
statement;
}
Style 2
Control{ Opening brace in a same line
statement;
}
19
Slide 20
20
myHCL My Learning –