Common Programming Mistakes

THE FOLLOWING LIST SUMMARIZES SOME OF the more common programming mistakes made in C.They are not arranged in any particular order. Knowledge of these mistakes will hopefully help you avoid them in your own programs.

1. Misplacing a semicolon.

Example

if ( j == 100 );

j = 0;

In the previous statements, the value of j will always be set to 0 due to the misplaced semicolon after the closing parenthesis. Remember, this semicolon is syntactically valid (it represents the null statement), and, therefore, no error is produced by the compiler.This same type of mistake is frequently made in while and for loops.

2. Confusing the operator = with the operator ==.

This mistake is usually made inside an if, while, or do statement.

Example

if ( a = 2 )

printf ("Your turn.\n");

The preceding statement is perfectly valid and has the effect of assigning 2 to a and then executing the printf call.The printf function will always be called because the value of the expression contained in the if statement will always be nonzero. (Its value will be 2.)

3. Omitting prototype declarations.

Example

result = squareRoot (2);

If squareRoot is defined later in the program, or in another file, and is not explicitly declared otherwise, the compiler assumes that the function returns an int.

Furthermore, the compiler converts float arguments to double, and _Bool, char,and short arguments to int. No other conversion of arguments is done.

Remember, it’s always safest to include a prototype declaration for all functions that you call (either explicitly yourself or implicitly by including the correct header file in your program), even if they’re defined earlier.

4. Confusing the precedences of the various operators.

Examples

while ( c = getchar () != EOF )

...

if ( x & 0xF == y )

...

In the first example, the value returned by getchar is compared against the value EOF first.This is because the inequality test has higher precedence than the assignment operator.The value that is therefore assigned to c is the TRUE/FALSE result of the test: 1 if the value returned by getchar is not equal to EOF, and 0 otherwise.

In the second example, the integer constant 0xF is compared against y firstbecause the equality test has higher precedence than any of the bitwise operators.

The result of this test (0 or 1) is then ANDed with the value of x.

5. Confusing a character constant and a character string.

In the statement

text = 'a';

a single character is assigned to text. In the statement

text = "a";

a pointer to the character string "a" is assigned to text.Whereas, in the first case,text is normally declared to be a char variable, in the second case, it should be declared to be of type "pointer to char".

6. Using the wrong bounds for an array.

Example

int a[100], i, sum = 0;

...

for ( i = 1; i <= 100; ++i )

sum += a[i];

Valid subscripts of an array range from 0 through the number of elements minus one.Therefore, the preceding loop is incorrect because the last valid subscript of a is 99 and not 100.The writer of this statement also probably intended to start with the first element of the array; therefore, i should have been initially set to 0.

7. Forgetting to reserve an extra location in an array for the terminating null character of a string.

Remember to declare character arrays so that they are large enough to contain the terminating null character. For example, the character string "hello" would require six locations in a character array if you wanted to store a null at the end.

8. Confusing the operator -> with the operator . when referencing structure members.

Remember, the operator . is used for structure variables, whereas the operator -> is used for structure pointer variables. So, if x is a structure variable, the notation x.m is used to reference the member m of x. On the other hand, if x is a pointer to a structure, the notation x->m is used to reference the member m of the structure pointed to by x.

9. Omitting the ampersand before nonpointer variables in a scanf call.

Example

int number;

...

scanf ("%i", number);

Remember that all arguments appearing after the format string in a scanf call must be pointers.

10. Using a pointer variable before it’s initialized.

Example

char *char_pointer;

*char_pointer = 'X';

You can only apply the indirection operator to a pointer variable after you have set the variable pointing somewhere. In this example, char_pointer is never set pointing to anything, so the assignment is not meaningful.

11. Omitting the break statement at the end of a case in a switch statement.

Remember that if a break is not included at the end of a case, then execution continues into the next case.

12. Inserting a semicolon at the end of a preprocessor definition.

This usually happens because it becomes a matter of habit to end all statements with semicolons. Remember that everything appearing to the right of the defined name in the #define statement gets directly substituted into the program. So the definition

#define END_OF_DATA 999;

leads to a syntax error if used in an expression such as

if ( value == END_OF_DATA )

...

because the compiler will see this statement after preprocessing:

if ( value == 999; )

...

13. Omitting parentheses around arguments in macro definitions.

Example

#define reciprocal(x) 1 / x

...

w = reciprocal (a + b);

The preceding assignment statement would be incorrectly evaluated as

w = 1 / a + b;

14. Leaving a blank space between the name of a macro and its argument list in the #define statement.

Example

#define MIN (a,b) ( ( (a) < (b) ) ? (a) : (b) )

This definition is incorrect, as the preprocessor considers the first blank space after the defined name as the start of the definition for that name. In this case, the statement

minVal = MIN (val1, val2);

gets expanded by the preprocessor into

minVal = (a,b) ( ( (a) < (b) ) ? (a) : (b) )(val1,val2);

which is obviously not what is intended.

15. Using an expression that has side effects in a macro call.

Example

#define SQUARE(x) (x) * (x)

...

w = SQUARE (++v);

The invocation of the SQUARE macro causes v to be incremented twice because this statement is expanded by the preprocessor to

w = (++v) * (++v);

【摘自《Programming in C》】

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,172评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,346评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,788评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,299评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,409评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,467评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,476评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,262评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,699评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,994评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,167评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,827评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,499评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,149评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,387评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,028评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,055评论 2 352

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,457评论 0 23
  • 有的时候,真的不知道自己为什么这么多愁善感,每次听到喜欢的歌都想分享给别人听,但是即使再好听的歌,在别人听来也可能...
    岛屿心情阅读 284评论 0 0
  • 大江东去,浪淘尽,剩下的银贝金珠,便是我们民族璀璨的中华文化。在这片古老的华夏大地上,兴衰更迭。随着潮起潮落...
    纪榣阅读 269评论 1 3
  • 2016-05-19 18:44:41121 “自序:不一样的教育”读后感 “莎士比亚说,世界是个...
    暖阳西子阅读 421评论 0 0
  • 她是个漂亮的姑娘。 我见她第一眼的时候就这样觉得。现在也还会絮絮叨叨地告诉别人,我认识一个很漂亮的姑娘,用很骄傲的...
    NiPai阅读 306评论 0 0