High-quality C ++/C Programming Guide
Lin Rui
Electronic Industry Press
After reading the book, I found a slightly different electronic version from the Internet and excerpted some chapters.
Electronic address http://man.chinaunix.net/develop/c&c++/c/c.htm
IfStatement
The IF statement is the simplest and most commonly used statement in C ++/C. However, many programmers write the if Statement by implicit errors. This section uses "compare with zero value" as an example to discuss.
4.3.1Comparison between Boolean variables and zero values
L[Rule 4-3-1]Boolean variables cannot be compared directly with true, false, or 1 or 0.
Based on the Boolean semantics, the zero value is "false" (marked as false), and any non-zero value is "true" (marked as true ). There is no uniform standard for the true value. For example, Visual C ++ defines true as 1, while Visual Basic defines true as-1.
Assuming that the Boolean variable is named flag, the standard if statement for comparing it with the zero value is as follows:
If (FLAG) // indicates that the flag is true.
If (! Flag) // indicates that the flag is false.
Other usage methods are poor, such:
If (flag = true)
If (flag = 1)
If (flag = false)
If (flag = 0)
4.3.2Comparison between Integer Variables and zero values
L[Rule 4-3-2]Integer variables should be replaced with "=" or "! = "Is directly compared with 0.
Assume that the integer variable is named value. The standard if statement for comparing it with zero value is as follows:
If (value = 0)
If (value! = 0)
The style of a Boolean variable cannot be imitated.
If (value) // you may misunderstand that value is a Boolean variable.
If (! Value)
4.3.3Comparison between floating point variables and zero values
L[Rule 4-3]Do not use "=" or "! = "Is compared with any number.
Please note that both float and double variables have precision restrictions. Therefore, do not use "=" or "! = "To the number, you should try to convert it into the form of"> = "or" <=.
Assume that the floating point variable is named X.
If (x = 0.0) // comparison of implicit errors
Convert
If (x> =-epsinon) & (x <= epsinon ))
Epsinon indicates the allowable error (accuracy ).
4.3.4Comparison between pointer variables and zero values
L[Rule 4-4-4]Use "=" or "! = "Is compared with null.
The zero value of the pointer variable is "null" (recorded as null ). Although the value of null is the same as that of 0, they have different meanings. Assume that the pointer variable is named P, and the standard if statement comparing it with zero value is as follows:
If (P = NULL) // Explicit Comparison between P and null emphasizes that p is a pointer variable
If (P! = NULL)
Do not write
If (P = 0) // It is easy to misunderstand that P is an integer variable.
If (P! = 0)
Or
If (p) // It is easy to misunderstand that p is a Boolean variable.
If (! P)
4.3.5Supplementary description of IF Statements
Sometimes we may see such an odd format as if (null = P. The program is not wrong. It is intended to put P and null upside down to prevent if (P = NULL) from being mistakenly written as if (P = NULL. The compiler considers if (P = NULL) as legal, but it indicates that if (null = P) is incorrect because null cannot be assigned a value.
Sometimes, if/else/return combinations are encountered in the program.
If (condition)
Return X;
Return y;
Rewrite
If (condition)
{
Return X;
}
Else
{
Return y;
}
Or rewrite it to a more concise
Return (condition? X: Y );
4.4Efficiency of loop statements
In C ++/C loop statements, the for statement is frequently used, while statements are rarely used. This section focuses on the efficiency of the cyclic body. The basic way to improve the efficiency of a circular body is to reduce the complexity of the circular body.
L[4-4-1 Recommended]In multiple cycles, if possible, the longest cycle should be placed in the innermost layer, and the shortest cycle should be placed in the outermost layer to reduce the number of times the CPU switches across the cycle layer. For example, Example 4-4 (B) is more efficient than Example 4-4 (.
For (ROW = 0; row <100; row ++) { For (COL = 0; Col <5; Col ++) { Sum = sum + A [row] [col]; } } |
For (COL = 0; Col <5; Col ++) { For (ROW = 0; row <100; row ++) { Sum = sum + A [row] [col]; } } |
Example 4-4 (a) low efficiency: long loop in the outermost layer Example 4-4 (B) High Efficiency: long loop in the innermost layer
L[4-4-2 recommended]If logical judgment exists in the loop body and the number of cycles is large, it is recommended to move the logical judgment outside the loop body. The program in Example 4-4 (c) executes the N-1 logical judgment more than in Example 4-4 (d. In addition, because the former still requires logical judgment and interrupts the loop "Pipeline" job, the compiler cannot optimize the loop and reduce the efficiency. If n is very large, it is best to use the example 4-4 (d) to improve efficiency. If n is very small, the efficiency difference between the two is not obvious. The example 4-4 (c) is better, because the process order is more concise.
For (I = 0; I <n; I ++) { If (condition) Dosomething (); Else Dootherthing (); } |
If (condition) { For (I = 0; I <n; I ++) Dosomething (); } Else { For (I = 0; I <n; I ++) Dootherthing (); } |
Table 4-4 (c) low efficiency but concise procedures Table 4-4 (d) high efficiency but not concise procedures
4.7 GOTOStatement
Since advocating structured design, Goto has become a controversial statement. First, because the GOTO statement can be flexibly redirected, without restrictions, it will indeed damage the structural design style. Second, Goto statements often cause errors or risks. It may skip some object construction, variable initialization, and important computing statements, such:
Goto state;
String S1, S2; // skipped by Goto
Int sum = 0; // skipped by Goto
...
State:
...
If the compiler cannot detect such errors, every time a GOTO statement is used, it may leave a hidden risk.
Many people suggest abolishing the C ++/c goto statement to eliminate future risks. But realistically speaking, errors are caused by programmers themselves, not by Goto's faults. At least one GOTO statement can be explicit. It can jump from multiple loops to the outside without writing many break statements. For example
{...
{...
{...
Goto error;
}
}
}
Error:
...
As if the building is on fire, it is too late to go down from the first level of the stairs, you can jump out of the fire pit from the window. Therefore, we advocate less use and careful use of the GOTO statement, rather than disabling it.
5.2 constComparison with # define
The C ++ language can use const to define constants, or use # define to define constants. However, the former has more advantages than the latter:
(1) const constants have data types, while macro constants do not. The compiler can perform type security checks on the former. However, only character replacement is performed for the latter, and there is no type security check. In addition, replacement may produce unexpected errors (marginal effect ).
(2) Some integrated debugging tools can debug const constants, but cannot debug macro constants.
L[Rule 5-2-1]In the C ++ program, only the const constant is used instead of the macro constant, that is, the const constant completely replaces the macro constant.