学习笔记之《Code Reading:The Open Source Perspective》Chapter two(part1)

本文介绍了C/C++编程中关于main函数参数argc和argv的理解,如何逐步解析函数的功能,以及如何正确使用break和continue等控制语句。同时,文章还提供了布尔表达式的简化方法,并提出了一些实用的编程建议。

Ø argc and argv

In C/C++ programs two arguments of the main function (customarily named argc and argv) are used to pass information from the operating system to the program about the specified command-line arguments. The argc variable contains the number of program arguments, while argv is an array of strings containing all the actual arguments (including the name of the program in position 0). The argv array is terminated with a NULL element, allowing two different ways to process arguments: either by counting based on argc or by going through argv and comparing each value against NULL.

Ø Strategies to understand a function

·        Guess, based on the function name.

·        Read the comment at the beginning of the function.

·        Examine how the function is used.

·        Read the code in the function body.

·        Consult external program documentation

Ø Gradual understanding

Understanding one part of the code can make others fall into place. Based on this form of gradual understanding you can employ a strategy for understanding difficult code similar to the one often used to combine the pieces of a jigsaw puzzle: start with the easy parts.

Ø break and continue

To determine the effect of a break statement, start reading the program upward from break until you encounter the first while, for, do,or switch block that encloses the break statement. Locate the first statement after that loop; this will be the place where control will transfer when break is executed. Similarly, when examining code that contains a continue statement, start reading the program upward from continue until you encounter the first while, for, or do loop that encloses the continue statement. Locate the last statement of that loop; immediately after it (but not outside the loop) will be the place where control will transfer when continue is executed.

From Andy:The meaning above can be simply expressed by the following examples

Ex:

for(a;b;c):
        a-->b-->...-->continue-->c-->b-->...

while(exc): 
        while(exc)-->...-->continue-->while(exc)-->...
do-while (exc):
        do -->...-->continue-->while(exc)-->...

Ø Boolean expressions

it is sometimes useful to transform Boolean expressions to a more readable form. If, for example, we wanted to translate the expression into the range membership expression we used above, we would need to substitute the logical OR with a logical AND (&&). This can easily be accomplished by using De Morgan's rules. De Morgan's rules provide you a quick and easy way to disentangle a complicated logical expression.

 

PS:De Morgan’s law

!(a || b) <=> !a && !b

!(a && b) <=> !a || !b

Ø Some tips

Variables that are visible to all program files (that is, not declared as static) can interact in surprising ways with variables with the same name defined in other files. It is therefore a good practice when inspecting code to ensure that all variables needed only in a single file are declared as static.

Ø Notice

1.   Calling strcmp or any other string function and passing it a NULL value instead of a pointer to actual character data will cause a program to crash in many operating environments.

2.   Not checking the result of output operations can cause a program to silently fail, losing output without any warning. Checking the result of each and every output operation can be inconvenient. A practical compromise you may encounter is to check for errors on the standard output stream before the program terminates.

3.   Continue ignores switch statements and that neither break nor continue affect the operation of if statements.

 

 

Andy 编辑于2010-07-17   19:06:55

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值