Professional Documents
Culture Documents
รูปแบบและมาตรฐานการเขียนโปรแกรมภาษาซี
รูปแบบและมาตรฐานการเขียนโปรแกรมภาษาซี
Much appreciation are due to the authors of Recommended C Style and Coding Standards, namely, L.W.
Cannon, R.A. Elliott, L.W. Kirchhoff, J.H. Miller, J.M. Milner, R.W. Mitze, E.P. Schan, and N.O. Whittington
of Bell Labs; Henry Spencer of Zoology Computer Systems, University of Toronto; David Keppel, EECS,
UC Berkeley/CS&E, University of Washington; Mark Brader of SoftQuad Incorporated, Toronto. The
stupendous repertoire and years of combined experience of the authors yielded this invaluable guideline
for better practice of C programming as described in the abstract of the Style Guide:
“This document is an updated version of the Indian Hill C Style and Coding Standards
paper, with modifications by the last three authors. It describes a recommended coding
standard for C programs. The scope is coding style, not functional organization.”
My special thank goes to David Keppel who grants permission to translate the Style Guide into Thai and
whose advice (in our email correspondence) certainly is one novice C programmers should heed:
“I think the most important lesson is: code with one consistent style. Even if it's a ‘bad’
style, at least someone reading the code can pick up the style and then not get further
confused by it.”
This Style Guide will not be complete without the famous “The Ten Commandments for C Programmers”
by Henry Spencer. With permission from Henry Spencer, the original version of The Ten Commandments
was replaced by his “Annotated” version to end the translated style guide on a high note.
I sincerely hope that this translation will instill many more Thai programmers to become aware of the
essence of good programming practices as I myself have long been inspired by numerous research
works of the aforementioned authors.
Peraphon Sophatsathit
Bangkok, Thailand
May 1, 1997
หนา
Acknowledgments....................................................................................................................... 2
คํานํา ............................................................................................................................................ 4
บทนํา............................................................................................................................................ 5
1. Introduction .......................................................................................................................... 6
2. File Organization .................................................................................................................. 6
3. Comments ............................................................................................................................ 9
4. การประกาศตัวแปร (Declarations)......................................................................................... 10
5. Function Declarations .......................................................................................................... 12
6. Whitespace .......................................................................................................................... 13
7. Examples.............................................................................................................................. 14
8. Simple Statements ............................................................................................................... 15
9. Compound Statements ........................................................................................................ 17
10. Operators ............................................................................................................................. 20
11. กฎเกณฑการตั้งชื่อ (Naming Conventions)............................................................................ 21
12. คาคงที่ (Constants) ............................................................................................................... 21
13. Macros.................................................................................................................................. 22
14. Conditional Compilation....................................................................................................... 23
15. Debugging ........................................................................................................................... 24
16. Portability.............................................................................................................................. 25
17. ANSI C.................................................................................................................................. 32
18. ขอควรคํานึงพิเศษ (Special Considerations) ......................................................................... 35
19. Lint........................................................................................................................................ 36
20. Make..................................................................................................................................... 36
21. มาตรฐานเฉพาะงาน (Project-Dependent Standards) ........................................................... 37
22. บทสรุป (Conclusion)............................................................................................................. 37
บรรณานุกรม (References)........................................................................................................... 39
The Ten Commandments for C Programmers............................................................................ 40
แทนที่จะเขียนผิดๆเปนเพียง
พีระพนธ โสพัศสถิตย
1 พฤษภาคม 2540
main()
{
printf(“hello world\n”);
}
int
main(void)
{
printf(“hello world\n”);
return 0;
}
เพราะ ANSI C กําหนดวา main ในภาษา C จะตอง return คา int เสมอ เพียงแคนี้ก็จะเห็นวาโปรแกรมภาษา C ที่
เห็นๆ กันก็ผิดไปจากมาตรฐานกวาครึ่ง เพราะโปรแกรมเหลานั้นมักจะใช void แทน int ซึ่งเปนคาที่ควรจะ return
โดย main ที่กลาวขางตนเปนเพียงตัวอยางงายๆ โดยชี้ใหเห็นวา วิวัฒนาการ และความนิยมของภาษา ไดเพิ่มความ
สลับซับซอนใหกับโปรแกรมภาษา C มาก สิ่งที่โปรแกรมเมอรทุกคนควรจะคํานึงถึงก็คือ “เขียนโปรแกรมใหอานงาย
1. Introduction
โดยเหตุที่ ANSI ไมไดกําหนดรูปแบบการเขียนโปรแกรมอยางเปนทางการ จึงไมมีขอกําหนดกฎเกณฑอะไร
วา การเขียนโปรแกรมภาษา C แบบไหนจึงจะถูกตองตามหลักการ 100 เปอรเซ็นต มาตรฐานรูปแบบของโปรแกรม
ในภาษา C ที่จะกลาวถึงในที่นี้ เปนมาตรฐานที่เรียกวา Indian Hill Style ซึ่งเปนมาตรฐานที่ไดรับความนิยมอยาง
แพรหลายในหมูผูพัฒนาโปรแกรมภาษา C มาตรฐานดังกลาวกําหนดโดย Bell Labs และผูทรงคุณวุฒิใน
คณะอนุกรรมการของ ANSI C อีก 3 ทาน เราจะมาดูรายละเอียดของหลักการแตละขั้นของมาตรฐานดังกลาว
“To be clear is professional; not to be clear is unprofessional.”⎯Sir Ernest Gowers.
2. File Organization
ขอเริ่มจากการจัดการเกี่ยวกับ file ซึ่งประกอบดวยสวนตางๆ แยกจากกันดวยการเวนบรรทัด แมวาจะไมมี
การกําหนดขนาดใหญที่สุดที่ควรจะเปน file หนึ่งๆ ไมควรยาวเกิน 1000 บรรทัด เพราะทั้ง editor และ compiler
จะทํางานลําบาก การใสเครื่องหมายดอกจันทนเปนตัวคั่นระหวางบรรทัด ทําใหเสียเวลาในการ scroll จึงไมแนะนํา
ใหใช บรรทัดใดที่ยาวเกิน 79 ตัวอักษร (columns) ไมแนะนํา เพราะปญหาเกี่ยวกับการแสดงผลของจอ บรรทัดที่
ยาวมากๆ อันเนื่องมาจากการยอหนา (indent) หลายระดับ บอกถึงการจัดรูปแบบโปรแกรมที่ไมดี
#ifndef EXAMPLE_H
#define EXAMPLE_H
... /* body of example.h file */
#endif /* EXAMPLE_H */
/*
* This file contains two functions, namely, proc_student and
* do_report. Each function, in turn, calls a number of header
* files included in a user-defined header allfile.h.”
* The following...
*/
#include “allfile.h”
#include “local_n.h”
#define SIZE 40
#define LENGTH 80
typedef struct student
{
char f_name[SIZE];
char L_name[LENGTH];
double GPA;
int credit;
} STUDENT_REC;
int max_student; /* global variable */
void proc_student(void); /* function prototype */
void do_report(STUDENT_REC);
3. Comments
“When the code and the comments disagree, both are probably wrong.”⎯Norm Schryer
เรื่องตอไปที่จะพูดถึง ซึ่งทุกคนมักจะมองขาม หรือไมใหความสนใจเทาที่ควรคือ comment การใส
comment ในโปรแกรมซึ่งเปนสวนหนึ่งของ documentation เปนสิ่งที่ตองทํา ไมใชควรทํา เพราะ comment จะบอก
วา โปรแกรมทําอะไร ทําอยางไร มี parameters อะไรที่เกี่ยวของ และถูกเปลี่ยนแปลงคาอยางไร ขอจํากัดของแตละ
block มีอะไรบาง เชน while จะทํางานจนกวาจะถึง EOF หรือ จํานวน records มากกวา MAX_NO เปนตน ควร
ระวังการ comment ที่เกินความจําเปน เชน
/*
* This is a block of comment
*/
/*
** Alternate format for block of comments
*/
4. การประกาศตัวแปร (Declarations)
ตัวแปรประเภท global ควรจะเริ่มตนที่ column 1 ตัวแปรชนิด external ตองเริ่มตนดวย keyword extern
ถาตัวแปรชนิด extern เปน array ที่มีขนาดแนนอน ตองกําหนดขนาดของ external array นั้นทุกครั้งไป นอกเสียจะ
กวาขนาดนั้นถูกกําหนดไวเรียบรอยแลวในตัวมันเอง (เชน read-only character array ที่เปน null-terminated) การ
กําหนดขนาดไวทุกครั้งเชนนี้ เปนประโยชนตอผูที่อานโปรแกรมที่คนอื่นเขียนไดงายขึ้น
ตัวกํากับชนิดตัวแปรประเภทหนึ่ง คือ pointer (สัญลักษณ `*') ควรจะเขียนติดกับชื่อตัวแปร ไมใชประเภท
(type) ของตัวแปลที่หลายๆ คนทํากัน กลาวคือ
ไมใชเปน
char* s, t, u;
struct mydef
{
int length; /* column 1 */
int occurs; /* Size of struct */
char code; /* data code */
};
#define ขางตนมักจะวางไวใต struct ที่เกี่ยวของ ในกรณีนี้คือ struct mydef หรือจะใสไวภายใน {...} ของ
struct mydef ก็ได ในบางกรณี enum อาจจะมีประโยชนกวา #define ก็ได เชน
int x = 5;
char *msg = “hello world”;
struct mydef array[] =
{
{20, MIN, 6000000L},
{8, SECOND, 0L},
{0},
};
ชนิดของคาที่ return โดย function หนึ่งๆ ควรจะประกาศใหชัดเจน (แมวาจะเปน int ก็ตาม) ถา compiler
supports prototype ก็เขียน prototype ดวย ขอผิดพลาดที่พบบอยๆ ก็คือ การละเลยการประกาศชนิดของคาที่
return จาก external math functions ซึ่งมักจะเปน double Compiler สวนมากจะอนุมานวา function ดังกลาว
return คา int (ในกรณีที่ไมประกาศวาเปน double) ซึ่งทําใหคาที่ return ถูก convert เปนคาของตัวแปรแบบ floating
ที่ผิดความหมาย
“C takes the point of view that the programmer is always right.”⎯Michael DeCorte
5. Function Declarations
ในการเขียน function ควรเริ่มตนดวย comment ซึ่งเปนบทนําในการกลาวถึงหนาที่ของ function นั้น และ
วิธีการ (เรียก) ใช อาจจะรวมถึงการตัดสินใจเกี่ยวกับหลักการออกแบบที่สําคัญๆ และผลกระทบขางเคียง (side
effects) ของ function นั้น ไมตองกลาวซ้ําถึงสิ่งที่มองเห็นไดชัดจาก code ของ function
คาที่ function สงคืนควรจะใสไวเปนบรรทัดเดียวตางหาก ซึ่งอาจจะยอหนา 1 tabstop ก็ได อยาปลอยให
default เปนคา int ถา function ไมได return คาอะไรใหใช void (สําหรับ compiler ที่ไม support void ก็ใหใช
#define void หรือ #define void int แทนคา void) ถาคาที่สงคืนตองมีคําอธิบายเพิ่ม เพื่อความกระจางควรจะ
อธิบายใน comment ขางตน ถาไมยาวมากก็อาจจะตอทาย (บนบรรทัดเดียวกัน) คา return ก็ได โดยเวนวรรคสัก 1
tabstop ชื่อ function และ formal parameter list ควรจะอยูบนบรรทัดเดียวโดดๆ โดยเริ่มที่ column 1 local
variable และ code ของ function ควรจะยอหนา 1 tabstop วงเล็บปกกาเปดและปดควรอยูบนบรรทัดเดียวโดดๆ
และเริ่มที่ column 1
โดยทั่วไป หนาที่ของ variable แตละตัวควรจะอธิบายใหชัดเจน ซึ่งอาจจะกระทําไดโดยใสใน comment
ขางตน หรือตอทายบนบรรทัดเดียวกับ variable (วิธีหลังไมแนะนํา) loop counter “i”, pointers ประเภท strings
“s”, และ int ที่ใชเปน character “c” มักจะไมตองอธิบาย (ตามความนิยม) ถากลุมของ function ใดที่มี parameter
6. Whitespace
int i;main(){for(;i[“]<i;++i){--i;}”];read('-'-'-',i+++”hell\
o, world!\n”,'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
- Dishonorable mention, Obfuscated C Code Contest, 1984.
Author requested anonymity.
if(foo->next==NULL&&totalcount<needed&&needed<=MAX_ALLOT&&
server_active(current_input))
{
...
}
c = (a == b)
? d + f(a)
: f(b) - d;
7. Examples
มาดูตัวอยางงายๆ กันสักตัวอยาง
8. Simple Statements
การเขียนคําสั่งในภาษาซี จะแยกเปนคําสั่งๆ หรือที่เรียกวาประโยค (statement) ซึ่งจะจบดวย “;” ที่ทาย
ประโยคเสมอ ประโยคในภาษาซีแบงออกเปน 2 ชนิด คือ simple statements และ compound statements
ในการเขียน simple statement นั้น ควรจะจัดใหมีบรรทัดละ 1 ประโยคเทานั้น ยกเวนวาแตละประโยคมี
สวนเกี่ยวของกันอยางใกลชิด เชน
ตัวอยางประโยคพิเศษที่ควรจะเขียนอยูบนบรรทัดเดียวโดดๆ คือ null body ของ for หรือ while loop เพื่อแสดงให
ผูอานเห็นวาเปนความตั้งใจของโปรแกรมเมอร ไมใชลืม เชน
ไมควรจะรวมเปน
d = (a = b + c) + r;
for (...)
{
while (...)
{
if (disaster)
goto error;
}
}
...
error:
clean up the mess
9. Compound Statements
กลุมของคําสั่งที่ลอมรอบดวยวงเล็บปกกา รวมเรียกวา compound statement ตําแหนงของการวางวงเล็บ
ปกกามีหลายรูปแบบ ทําใหเกิด style มากมาย หลักสําคัญคือ ยึด style ชนิดใดชนิดหนึ่งเปนหลัก (ตามมาตรฐานที่
ถนัด) ในการเขียนโปรแกรม เชน ในการแกไขโปรแกรมของผูอื่น ควรจะยึด style ที่ใชในโปรแกรมนั้น จะไดมีรูปแบบ
ที่เสมอตนเสมอปลายโดยตลอด ดังตัวอยาง
(control อาจจะแทน if, for, หรือ while ฯลฯ) Style ขางตนเรียกวา “K&R Style” ซึ่งเปนที่นิยมใชกันมาก โดยเฉพาะ
สําหรับผูที่ยังไมมี style ของตนเอง ในการเขียนแบบ K&R Style นั้น สวนอนุประโยค else ใน if-else และ while
ใน do-while จะอยูบรรทัดเดียวกับวงเล็บปกกาเปด แตสําหรับ style อื่น วงเล็บปกกาจะอยูบนบรรทัดเดียวโดดๆ
เชน (จะแสดงใหดูเฉพาะ 3 ตัวอยางขางลางเทานั้น ผูเขียนนิยมอีก style)
if (condition) {
statement;
statement;
} else
statement;
do {
statement;
statement;
} while (condition);
switch (expression) {
case ABC:
case DEF:
statement;
break;
case UVW:
statement;
/* fall-through */
case XYZ:
statement;
break;
}
if (expr1)
if (expr2)
funca();
else
funcb();
ควรจะเขียนเปน
if (expr1)
{
if (expr2)
{
funca();
}
}
else
{
funcb();
}
if (STREQ(reply, “yes”))
{
statements for yes;
}
else if (STREQ(reply, “no”))
{
statements for no;
}
else if (STREQ(reply, “maybe”))
{
statements for maybe;
}
else
{
statements for default;
}
#define CIRCUIT
# define CLOSE_CIRCUIT(cirno) { close_circ(cirno); }
#else
# define CLOSE_CIRCUIT(cirno)
#endif
โดยในโปรแกรมมีการเรียกใชดังนี้
if (expr)
statement;
else
CLOSE_CIRCUIT(x)
++i;
10. Operators
unary operator ไมควรเขียนแยกจาก operand ที่เปนตัวเดียว โดยทั่วไป binary operator จะใช 1 blank
คั่นระหวาง operand ยกเวน “.” และ “->“ สําหรับ expression ที่มีความสลับซับซอนมาก ก็ตองใชวิจารณญาณที่
เหมาะสม เชน operators ระดับในไมใส blank แตไปใสในระดับนอก เปนตน
ถาคิดวา expression นั้นจะอานยาก ลองเขียนตอบรรทัดไป การแยกก็ถือเอาชวง operator ที่มี
precedence ต่ําสุดเปนเกณฑจะดีที่สุด เนื่องจาก C มีกฎขอยกเวนที่เกี่ยวกับ precedence, expression ใดที่
ประกอบดวย operators หลายๆ ชนิดผสมกัน ควรจะใสวงเล็บ แตระวังอยาใชวงเล็บซอนๆ กันมาก เพราะสายตา
คนไมเกงในเรื่องการจับคูวงเล็บ
ในบางครั้ง comma operator ก็มีความจําเปนเชนกัน แตโดยทั่วไป ถาเลี่ยงไมใชก็จะเปนการดี comma
operator เหมาะสมสําหรับ multiple initializations และ operations อยางเชนในคําสั่ง for ในกรณีของ expression
ที่ซับซอน เชน พวกที่มี ternary operator ?: ซอนอยูภายใน ไมควรจะเพิ่ม comma operator ใหเกิดความสับสนมาก
จะเหมาะสมกวา
เพราะ front_door เปน pointer ในการเปรียบเทียบคา pointer ใดๆ ใหใช NULL แทน 0 เพราะ NULL เปนคาคงที่
จาก standard I/O library header (stdio.h หรือ stdlib.h) แมแตในกรณีที่ใช 1 หรือ 0 โดดๆ ก็ควรใช #define เปน
TRUE หรือ FALSE (บางครั้งอาจเปลี่ยนเปน YES หรือ NO จะเขาใจดีกวา)
Character constant แบบงายๆ ควรใช character literal แทนที่จะเปนตัวเลข (หมายถึง ASCII code)
character ที่เปน non-text ไมควรใชอยางยิ่ง เพราะไม portable ถาหลีกเลี่ยงไมได โดยเฉพาะเมื่อเปนสวนหนึ่งของ
string จะเขียนในรูปของ escape character ดวย octal digit สามตัวแทนที่จะเหลือตัวเดียว (เชน ‘\007’) ถึงกระนั้น
ก็ดีวิธีดังกลาวก็ยังถือวาเปน machine-dependent อยูมาก
13. Macros
Expressions ที่ซับซอนอาจจะทําเปน macro ที่เรียกใช parameters แตตองระวังเกี่ยวกับปญหาของ
operator precedence วิธีแกคือใสวงเล็บรอบ parameter ทุกตัว ซึ่งเปนวิธีเดียวที่จะปองกันการเกิด side effects
ขอควรปฏิบัติอีกประการหนึ่งคือ ควรเขียน macro ที่ evaluate parameter เพียงครั้งเดียว เพราะ macro สวนมาก
ทํางาน ตางจาก function ทั่วๆ ไป
Macro บางกลุมอาจอยูในรูปของ function เชน getc และ fgetc ควรใช macro ในการสราง function
เพราะการเปลี่ยนแปลงใดๆ ใน macro จะสงผลถึงตัว function โดยอัตโนมัติ แตตองระวังการใช parameters ใหดี
ระหวาง macro กับ function เพราะ parameters ที่สงใหแก function เปนแบบ by-value เสมอ (C ไมมีการสง
parameter แบบ reference - ผูเขียน) แต macro สงแบบ name-substitution การเขียน macro ที่ปราศจาก side
effects จึงตองคํานึงถึงการประกาศรูปแบบของ macro นั้นๆ อยางชัดเจน ตัวแปรที่ใชใน macro ควรหลีกเลี่ยงตัว
แปรชนิด global เพราะ global เหลานั้น อาจถูกเลื่อนไป เนื่องจากชื่อที่อยูใน local บางจุด (ที่มีชื่อซ้ํากัน) macro ที่
ใชชื่อตัวแปร หรืออางในดานซายมือของ assignment statement ควรจะใส comment กํากับไว สวน macro ที่ไมใช
parameters แตอางถึงตัวแปร หรือเปน alias ของ function call ไมควรใส parameter เลย เชน
if (x == 3)
SP3();
else
BORK();
ถาไมใส semicolon หลัง SP3 สวนของ else จะกลายเปนสวนหนึ่งของ macro SP3 ซึ่งผิด logic ของโปรแกรมเดิม
อันที่จริงแลว macro SP3 สามารถเขียนใหมเปน
#define SP3() \
do { if (b) { int x; av = f(&x); bv += x; } } while(0)
#ifdef lint
static int ZERO;
#else
# define ZERO 0
#endif
#define STMT(stuff) do {stuff} while(ZERO)
SP3 สามารถเขียนใหมเปน
#define SP3() \
STMT( if (b) { int x; av = f(&x); bv += x; } )
#ifdef DEBUG
extern void *mm_malloc();
# define MALLOC(size) (mm_malloc(size)
#else
extern void *malloc();
# define MALLOC(size) (malloc(size))
#endif
#ifdef BSD4
long t = time((long *)NULL);
#endif
15. Debugging
“C Code, C code run. Run, code, run...PLEASE!!!”⎯Barbara Tongue
ถาตองการใช enum ควรจัดให enum constant ตัวแรกมีคาไมเปนศูนย หรือเปนคาบอกวาเกิด error เชน
#ifdef DEBUG
# define MALLOC(size) (mm_malloc(size))
#else
# define MALLOC(size) (malloc(size))
#endif
16. Portability
“C combines the power of assembler with the portability of assembler.”⎯Anonymous, alluding to Bill
Thacker.
ขอดีของ code ที่ portable นั้นเปนที่ทราบกันดีในวงการของการเขียนโปรแกรม ในสวนนี้จะกลาวถึง
แนวทางของการเขียน portable code คําวา “portable” ในที่นี้หมายถึง source file ที่สามารถนําไป compile และ
execute บนเครื่องตางกัน เพียงแคเปลี่ยน header files และ compiler flags ที่แตกตางกันเทานั้น Header files
สวนมากจะประกอบดวย #define และ typedef ซึ่งไมเหมือนกันในทุกๆเครื่อง โดยทั่วไปแลว เมื่อพูดถึง “เครื่องใหม”
มักจะหมายถึงฮารดแวรคนละชนิด ระบบปฏิบัตกิ ารตางกัน compiler ตางกันหรือทุกอยางที่กลาวมาทั้งหมดตางกัน
โดยสิ้นเชิง เอกสารอางอิง [1] กลาวถึง style และ portability ที่ดีหลายประการ ขอแนะนําสําหรับใชเปนแนวทางใน
การปองกันไมใหเกิดความผิดพลาดเนื่องจากการ port โปรแกรมมีดังนี้
1. เขียนโปรแกรมที่ portable กอน โดยไมตองกังวลถึงรายละเอียดของการ optimize นอกเสียจากจะหลีกเลี่ยง
ไมได โปรแกรมที่ optimized มักจะอานยาก การ optimize บนระบบของเครื่องหนึ่งอาจกลายเปน code ที่
แยลงสําหรับระบบของอีกเครื่องหนึ่ง พยายามบันทึกการแกไขทุกอยางที่เกี่ยวของกับการปรับปรุง
performance โดยจัดกลุมใหอยูใกลกันมากที่สุด (เพื่อไมใหเกิด side effects ตอ code สวนอื่น) การบันทึก
ควรจะอธิบายเหตุผล ความจําเปน และหลักการทํางาน
2. ระลึกเสมอวาบางสิ่งบางอยางนั้น port ไมได เชน code ที่ตองจัดการเกี่ยวกับ register คาของ program
status word หรือ code ที่ออกแบบเพื่อควบคุมการทํางานของฮารดแวรเฉพาะงาน เชน I/O drive และ
assembler ซึ่งในกรณีหลังมีหลายสวนที่สามารถทําเปน machine independent ได
11. Floating point มีทั้ง precision และ range ซึ่งไมขึ้นกับขนาดของ object ดังนั้น เมื่อเลข floating-point ชนิด
32-bit เกิด overflow (หรือ underflow) ขึ้น มักจะเกิดที่คาตางกันบนฮารดแวรตางชนิดกัน เชน 4.9 คูณ 5.1
อาจจะใหผลลัพธตางกันบนฮารดแวรตางชนิดกัน ซึ่งความแตกตางจากการปดเศษขึ้นหรือลง อาจสงผลลัพธ
ที่ไมคาดคิดมากอนได
12. บนฮารดแวรบางชนิด double อาจจะใหจํานวน precision นอยกวา float ได
13. บนฮารดแวรบางชนิด ครึ่งแรกของ double อาจจะมีคาเปน float ที่มีคาเหมือนกัน แตอยายึดเปนหลักเกณฑ
มาตรฐาน
14. ระวังการใช signed character เพราะบนฮารดแวร VAX บางรุน character จะถูก signed extended เมื่อใช
ใน expression ลักษณะดังกลาวไมเกิดกับฮารดแวรชนิดอื่น โปรแกรมที่ยึดสมมุติฐานของ
signed/unsigned นั้นไม portable เชน array[c] อาจจะใหคําตอบผิดพลาด ถาคา c ที่คิดวาจะมีคาเปน
บวกกลับกลายเปนลบ (เพราะเปน signed) ถาจําเปนตองใช signed หรือ unsigned character จริงๆ ให
เขียน comment วาเปน SIGNED หรือ UNSIGNED ผลลัพธที่ไดจาก unsigned จะถูกตองเสมอก็ตอเมื่อใช
unsigned char เทานั้น
c = foo(getchar(), getchar());
char
foo(char c1, char c2, char c3)
{
char bar = *(&c1 + 1);
return bar; /* often won’t return c2 */
}
s = “/dev/tty??”;
strcpy(&s[8], ttychars);
long i;
short a[N];
i = old;
i = a[i] = new;
บางครั้งจะถูกแทนคาเปน
(printf(“filename=%s”,(filename)))
ตัวอยางที่สองจะถูกแทนคาไดสองลักษณะ ทําใหโปรแกรมทํางานไมเปนไปตามที่คาดไว
31. ศึกษาทําความคุนเคยกับ library functions และ defines (แตอยางลงลึกมาก เพราะรายละเอียดของการ
ทํางานที่ไมเกี่ยวกับ interface ภายนอก อาจมีการเปลี่ยนแปลงแกไขโดยไมมีการแจงลวงหนา รายละเอียด
สวนมากมักจะไม portable ดวย) ไมควรจะเขียน routines ที่เปรียบเทียบ string, terminal control หรือ
กําหนด system structures เอง การทําเองเปนการเสียเวลาโดยใชเหตุ และทําใหโปรแกรมอานเขาใจยาก
เพราะคนอื่นตองคอยพะวงวาโปรแกรมสวนที่เราเขียนเองมีอะไรที่แตกตางจากสิ่งที่มีอยูแลว และยังเปนการ
กีดขวางการติดตอใชประโยชนจาก microcode หรือเครื่องชวยอื่นๆของ system routines ในกรณีที่กําหนด
system structure ตางออกไปเอง นอกจากนี้สิ่งที่กําหนดขึ้นเองยังเปนทางนําไปสู bug ของโปรแกรม ถา
เปนไปได ศึกษาความแตกตางระหวาง common libraries ของ ANSI และ POSIX
32. พยายามเรียกใช lint ถามีใหใชเพราะเปนเครื่องมือที่ชวยในการหา machine-dependent รูปแบบตางๆ
รวมทั้งขอขัดแยงหรือ bugs ที่อาจหลุดรอดการตรวจของ compiler ถา compiler มี switch ที่จะเพิ่มการสง
warning ใหทราบใชใหเปนประโยชนมากที่สุด
33. ตั้งขอสงสัย label ที่อยูภายใน block ที่อาจเกี่ยวพันกับ switch หรือ goto นอก block นั้น
34. เมื่อใดก็ตามที่เกิดความไมแนใจเกี่ยวกับ type ของ parameters ให cast เปน type ที่เหมาะสม ควรจะ cast
NULL ทุกครั้งที่ปรากฏใน non-prototyped function calls อยาพยายามอาศัย function calls เปนสื่อในการ
เปลี่ยนชนิด (type) ของ parameters เพราะ C มีกฎการ promote ชนิดของตัวแปรที่เขาใจยาก เชน ถา
function หนึ่งตองการคา 32-bits long แตกลับได 16-bit int stack ของ function อาจจะเกิดการเรียง
ขนาดผิด (misaligned) คาที่สงผานจะเกิดการ promote ผิดไป
35. ใช explicit cast เมื่อมีการคํานวณที่ผสมระหวางคา signed และ unsigned
36. การกระโดดระหวาง procedure เชน longjump ควรจะใชอยางระมัดระวัง สวนมากมักจะลืม restore คา
ใน registers พยายามประกาศคาที่มีความสําคัญยิ่งยวด (critical values) เปนชนิด volatile ถาทําได หรือ
อยางนอยก็ใส comment ใหเปน VOLATILE
37. Linkers บางตัวเปลี่ยนชื่อเปนตัวอักษรเล็ก (lowercase) บางตัวรับแค 6 ตัวอักษรแรกวาเปน unique
เพราะฉะนั้นโปรแกรมอาจจะทํางานผิดโดยที่ไมมีอะไรกระโตกกระตากบนเครื่องเหลานี้
38. พึงระวังสิ่งที่นอกเหนือจากกติกา (compiler extensions) ถาจําเปนตองใชจริงๆ บันทึกไวอยางละเอียดและ
ถือเปนรูปแบบของ machine dependencies
39. โดยทั่วไป โปรแกรมจะไมทํางานในสวนของ data segment หรือ บันทึกผลลัพธลงใน code segment แมวา
บางกรณีอาจทําไดแตไมมีอะไรที่จะรับประกันวาโปรแกรมจะทําไดถูกตองเสมอไป
17.1 Compatibility
พยายามเขียน code ที่ port ไปสู compiler รุนเกาไดงาย เชน ใช conditional #defined ในการกําหนด
keywords ตางๆ ใน global header files อาทิ const, volatile ฯลฯ Compiler มาตรฐานกําหนดสัญลักษณสําหรับ
preprocessor ที่เรียกใชไดตลอด คือ __STDC__ สําหรับ void* เปนชนิดที่จัดการยากหนอยเพราะ compiler รุน
เการูจักแต void วิธีงายๆ คือสราง type ใหมที่เปน machine และ compiler-dependent เชน VOIDP ในรูปของ
char* บน compiler รุนเกา ดังตัวอยาง
#if __STDC__
typedef void *voidp;
# define COMPILER_SELECTED
#endif
#ifdef A_TARGET
# define const
# define volatile
# define void int
typedef char *voidp;
# define COMPILER_SELECTED
#endif
#ifdef ...
...
#endif
#ifdef COMPILER_SELECTED
# undef COMPILER_SELECTED
#else
{ NO TARGET SELECTED! }
#endif
prototype ของ functions รวมกับ parameter declaration และ parameter definition เปนบรรทัด
เดียวกัน parameter ทุกตัวควรจะบันทึกในสวน comment ของ function
/*
* ‘bp’: boat trying to get in.
* ‘stall’: a list of stalls, never NULL
* returns stall number, 0 => no room.
*/
int
enter_pier(boat_t const *bp, stall_t *stall)
{
...
}
17.3 Prototypes
ควรใช function prototype เพื่อให code มีความถูกตองมากที่สุด อีกทั้งยังชวยใหโปรแกรมทํางานเร็วขึ้น
เปนที่นาสังเกตวา การประกาศ prototype
void
bork(c)
char c;
{
...
}
void
bork(char c)
{
...
}
#if __STDC__
# define PROTO(x) x
#else
# define PROTO(x) ()
#endif
17.4 Pragmas
Pragmas ใชสําหรับการกําหนด machine-dependent code ในสวนที่ไม portable (เพราะผูกติดอยูกับ
ฮารดแวร) รูปแบบของ ANSI pragmas ถูกกําหนดในลักษณะที่ยากตอการแยกออกจาก pragmas อื่นๆ เพื่อรวมไว
ใน machine-dependent headers แหงเดียว
Pragmas มีสองกลุมคือ optimization ซึ่งอาจจะละเลยโดยไมมีผลกระทบแตอยางใด กับ pragma ที่เปน
พฤติกรรมของการทํางานระบบ (required pragmas) ซึ่งไมอาจจะละเลยได pragmas กลุมหลังควรจะคุมดวย
#ifdef เพื่อหยุดการ compile โปรแกรม ถา required pragma ขาดหายไป
Compiler แตละตัวอาจทํางานตามคําสั่งของ pragma หนึ่งๆ ในลักษณะที่ตางกัน เชน ตัวแรกอาจจะใช
“haggis” ในการเริ่ม optimization ในขณะที่อีกตัวอาจจะใชเปนตัวบอกวา ถาประโยคที่กําหนดใหถูกเรียกใชเมื่อใด
ใหหยุดการทํางานโดยสิ้นเชิง ดังนั้น ทุกครั้งที่ใช pragmas จะตองคุมดวย #ifdef ที่เปน machine-dependent เสมอ
โดยเฉพาะกับ compiler ชนิด non-ANSI อยาลืมยอหนาบรรทัดที่เปน #pragma เพราะ preprocessor รุนเกา
อาจจะหยุดทํางานถาเจอคําสั่งดังกลาว เชน
abool = bbool;
if (abool) { ... }
19. Lint
Lint เปน C program checker [2, 11] ตัวหนึ่งที่ใชตรวจโปรแกรมภาษา C เพื่อรายงานจุดที่เกิด type ไม
ตรงกัน ความไมสมนัยระหวางนิยามของ function และตําแหนงที่เรียกใช จุดที่อาจจะเกิด bugs ในโปรแกรม ฯลฯ
การใช lint เปนสิ่งที่ควรทําประจํากับทุกๆ โปรแกรม ซึ่งโครงการสวนมากจะใช lint เปนเครื่องมืออันหนึ่งในการตรวจ
รับงาน
การใช lint ใหเกิดประโยชนสูงสุด ไมใชเพียงแคทําเปนเครื่องมือตรวจรับงาน แตควรจะถือเสมือนวาเปน
เครื่องชวยในการแกไขหรือเพิ่มเติม code ใหมๆ เพราะ lint สามารถหา bug ที่แฝงอยูในรูปการณที่คาดไมถึง และ
ประกันปญหาเกี่ยวกับ portability ไดดี Messages ตางๆ ที่เกิดจาก lint มักเปนตัวบอกถึงปญหาที่อาจจะเกิดขึ้น มี
เรื่องตลกเกี่ยวกับโปรแกรมที่ลืมใส argument ในคําสั่ง fprintf ดังนี้
20. Make
เครื่องมือที่มีประโยชนอีกชิ้นหนึ่ง คือ make [7] ในระหวางการพัฒนาโปรแกรม make จะชวย recompile
สวนของโปรแกรมที่ไดรับการแกไขใหม (จากการ make ครั้งลาสุด) เทานั้น Make ยังสามารถใชชวยในการทํางาน
อื่นๆ โดยอัตโนมัติ อาทิ
all ทําใหเปน binary ทั้งหมดเสมอ
clean ลบ intermediate file ทิ้ง
debug สราง binary “a.out” หรือ “debug” เพื่อใชทดสอบ
depend สราง transitive dependencies
[1] B.A. Tague, C Language Portability, Sept 22, 1977. This document issued by department 8234
contains three memos by R.C. Haight, A.L. Glasser, and T.L. Lyon dealing with style and
portability.
[2] S.C. Johnson, Lint, a C Program Checker, USENIX UNIX Supplementary Documents, November
1986.
[3] R.W. Mitze, The 3B/PDP-11 Swabbing Problem, Memorandum for File, 1273-770907.01MF,
September 14, 1977.
[4] R.A. Elliott and D.C. Pfeffer, 3B Processor Common Diagnostic Standards- Version 1,
Memorandum for File, 5514-780330.01MF, March 30, 1978.
[5] R.W. Mitze, An Overview of C Compilation of UNIX User Processes on the 3B, Memorandum for
File, 5521-780329.02MF, March 29, 1978.
[6] B.W. Kernighan and D.M. Ritchie, The C Programming Language, Prentice Hall 1978, Second
Ed. 1988, ISBN 0-13-110362-8.
[7] S.I. Feldman, Make - A Program for Maintaining Computer Programs, USENIX UNIX
Supplementary Documents, November 1986.
[8] Ian Darwin and Geoff Collyer, Can't Happen or /* NOTREACHED */ or Real Programs Dump
Core, USENIX Association Winter Conference, Dallas 1985 Proceedings.
[9] Brian W. Kernighan and P. J. Plauger, The Elements of Programming Style, McGraw-Hill, 1974,
Second Ed. 1978, ISBN 0-07-034-207-5.
[10] J. E. Lapin, Portable C and UNIX System Programming, Prentice Hall 1987, ISBN 0-13-686494-
5.
[11] Ian F. Darwin, Checking C Programs with lint, O'Reilly & Associates, 1989. ISBN 0-937175-30-7.
[12] Andrew R. Koenig, C Traps and Pitfalls, Addison-Wesley, 1989. ISBN 0-201-17928-8.
Henry Spencer
University of Toronto Zoology
henry@zoo.toronto.edu
1. Thou shalt run lint frequently and study its pronouncements with care, for verily its
perception and judgement oft exceed thine.
This is still wise counsel, although many modern compilers search out many
of the same sins, and there are often problems with lint being aged and
infirm, or unavailable in strange lands. There are other tools, such as Saber
C, useful to similar ends.
``Frequently'' means thou shouldst draw thy daily guidance from it, rather
than hoping thy code will achieve lint 's blessing by a sudden act of
repentance at the last minute. De-linting a program which has never been
lint ed before is often a cleaning of the stables such as thou wouldst not
wish on thy worst enemies. Some observe, also, that careful heed to the
words of lint can be quite helpful in debugging.
``Study'' doth not mean mindless zeal to eradicate every byte of lint output
if for no other reason, because thou just canst not shut it up about some
things but that thou should know the cause of its unhappiness and
understand what worrisome sign it tries to speak of.
2. Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end.
Clearly the holy scriptures were mis-transcribed here, as the words should
have been ``null pointer'', to minimize confusion between the concept of null
pointers and the macro NULL (of which more anon). Otherwise, the
meaning is plain. A null pointer points to regions filled with dragons,
3. Thou shalt cast all function arguments to the expected type if they are not of that type
already, even when thou art convinced that this is unnecessary, lest they take cruel
vengeance upon thee when thou least expect it.
Also, contrary to the beliefs common among the more backward inhabitants
of the Polluted Eastern Marshes, `NULL' does not have a pointer type, and
must be cast to the correct type whenever it is used as a function argument.
(The words of the prophet ANSI, which permit NULL to be defined as having
the type `void *', are oft taken out of context and misunderstood. The
prophet was granting a special dispensation for use in cases of great
hardship in wild lands. Verily, a righteous program must make its own way
through the Thicket Of Types without lazily relying on this rarely-available
dispensation to solve all its problems. In any event, the great deity Dmr who
created C hath wisely endowed it with many types of pointers, not just one,
and thus it would still be necessary to convert the prophet's NULL to the
desired type.)
4. If thy header files fail to declare the return types of thy library functions, thou shalt
declare them thyself with the most meticulous care, lest grievous harm befall thy
program.
The prophet ANSI, in her wisdom, hath added that thou shouldst also
scourge thy Suppliers, and demand on pain of excommunication that they
produce header files that declare their library functions. For truly, only they
know the precise form of the incantation appropriate to invoking their magic
in the optimal way.
The prophet hath also commented that it is unwise, and leads one into the
pits of damnation and subtle bugs, to attempt to declare such functions
thyself when thy header files do the job right.
5. Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where
thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
6. If a function be advertised to return an error code in the event of difficulties, thou shalt
check for that code, yea, even though the checks triple the size of thy code and
produce aches in thy typing fingers, for if thou thinkest ``it cannot happen to me'', the
gods shall surely punish thee for thy arrogance.
All true believers doth wish for a better error-handling mechanism, for
explicit checks of return codes are tiresome in the extreme and the
temptation to omit them is great. But until the far-off day of deliverance
cometh, one must walk the long and winding road with patience and care,
for thy Vendor, thy Machine, and thy Software delight in surprises and think
nothing of producing subtly meaningless results on the day before thy
Thesis Oral or thy Big Pitch To The Client.
7. Thou shalt study thy libraries and strive not to re-invent them without cause, that thy
code may be short and readable and thy days pleasant and productive.
Numberless are the unwashed heathen who scorn their libraries on various
silly and spurious grounds, such as blind worship of the Little Tin God (also
known as ``Efficiency''). While it is true that some features of the C libraries
were ill-advised, by and large it is better and cheaper to use the works of
others than to persist in re-inventing the square wheel. But thou should take
the greatest of care to understand what thy libraries promise, and what they
8. Thou shalt make thy program's purpose and structure clear to thy fellow man by using
the One True Brace Style, even if thou likest it not, for thy creativity is better used in
solving problems than in creating beautiful new impediments to understanding.
These words, alas, have caused some uncertainty among the novices and
the converts, who knoweth not the ancient wisdoms. The One True Brace
Style referred to is that demonstrated in the writings of the First Prophets,
Kernighan and Ritchie. Often and again it is criticized by the ignorant as
hard to use, when in truth it is merely somewhat difficult to learn, and
thereafter is wonderfully clear and obvious, if perhaps a bit sensitive to
mistakes.
While thou might think that thine own ideas of brace style lead to clearer
programs, thy successors will not thank thee for it, but rather shall revile thy
works and curse thy name, and word of this might get to thy next employer.
Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important. So it is with brace
style.
As a lamentable side issue, there has been some unrest from the fanatics of
the Pronoun Gestapo over the use of the word ``man'' in this Commandment,
for they believe that great efforts and loud shouting devoted to the ritual
purification of the language will somehow redound to the benefit of the
downtrodden (whose real and grievous woes tendeth to get lost amidst all
that thunder and fury). When preaching the gospel to the narrow of mind
and short of temper, the word ``creature'' may be substituted as a suitable
pseudoBiblical term free of the taint of Political Incorrectness.
Though some hasty zealots cry ``not so; the Millenium is come, and this
saying is obsolete and no longer need be supported'', verily there be many,
many ancient systems in the world, and it is the decree of the dreaded god
Murphy that thy next employment just might be on one. While thou sleepest,
he plotteth against thee. Awake and take care.
It is, note carefully, not necessary that thy identifiers be limited to a length of
six characters. The only requirement that the holy words place upon thee is
uniqueness within the first six. This often is not so hard as the belittlers
claimeth.
10. Thou shalt foreswear, renounce, and abjure the vile heresy which claimeth that ``All
the world's a VAX'', and have no commerce with the benighted heathens who cling to
this barbarous belief, that the days of thy program may be long even though the days
of thy current machine be short.
This particular heresy bids fair to be replaced by ``All the world's a Sun'' or
``All the world's a 386'' (this latter being a particularly revolting invention of
Satan), but the words apply to all such without limitation. Beware, in
particular, of the subtle and terrible ``All the world's a 32-bit machine'', which
is almost true today but shall cease to be so before thy resume grows too
much longer.