28. | and & statement execution sequence
------------
The operators "and" and "or" in the condition statement must be careful. Their performance may be different from what you think. Here, some actions in the Condition Statement need to be explained as follows:
Express1 | express2
If the expression express1 is true, express2 is not executed. express2 is executed only when express1 is false. Because the first expression is true and the whole expression is true, there is no need to execute the second expression.
Express1 & express2
If the expression express1 is "false", express2 will not be executed, and express2 will be executed only when express1 is "true. Because the first expression is false and the whole expression is false, there is no need to execute the second expression.
As a result, not all of the expressions you think will be executed. You must understand this. Otherwise, your program will encounter some runtime errors.
For example, the following program:
If (sum> 100 &&
(FP = fopen (filename, ""))! = NULL ){
Fprintf (FP, "warring: it beyond one hundred/N ");
......
}
Fprintf (FP, "sum is % ID/N", sum );
Fclose (FP );
Originally, if sum> 100, write an error message to the file. For convenience, write the two conditions together. Therefore, if sum <= 100, the file opening operation will not be performed. Finally, fprintf and fclose will find unknown results.
For another example, if I want to determine whether a character has content, I have to judge whether the string pointer is not null and its content cannot be empty (empty ), one is a null pointer and the other is a null content. I may write:
If (P! = NULL) & (strlen (p )! = 0 ))
Therefore, if p is null, strlen (p) will not be executed. Therefore, strlen will not "illegal operation" or "core dump" because of a null pointer.
Remember that not all statements in a condition statement are executed. When you have many condition statements, pay special attention to this.
29. Use for instead of while for loop as much as possible
---------------
Basically, for can complete the while function. I recommend that you use the for statement whenever possible instead of the while statement, especially when the loop body is large, the advantages of for are shown.
In for, the initial, ending condition, and progress of the loop are all together. At a glance, we can see what kind of loop this is. The program that just got out of school is generally like this for links:
P = phead;
While (p ){
...
...
P = p-> next;
}
When the while statement block becomes larger, your program will be hard to read, and it is much better to use:
For (P = phead; P = p-> next ){
..
}
At a glance, we know the starting condition, ending condition, and progress of the loop. You can understand what this cycle is about to do? In addition, it is easy to maintain the program. You don't have to go up and down in an editor like while.
30. sizeof type instead of Variable
-------------
Many programmers like sizeof variable names when using sizeof, for example:
Int score [100];
Char filename [20];
Struct userinfo USR [100];
When sizeof these three variable names, all return the correct results, so many programmers start to sizeof variable names. Although there is nothing wrong with this habit, I suggest the sizeof type.
I have seen this program:
Pscore = (int *) malloc (subject_cnt );
Memset (pscore, 0, sizeof (pscore ));
...
In this case, sizeof (pscore) returns 4 (pointer length) instead of the entire array. Therefore, memset Cannot initialize this memory. For ease of reading and maintenance of programs, I strongly recommend that you use types instead of variables, such:
For score: sizeof (INT) * 100/* 100 int */
For filename: sizeof (char) * 20/* 20 char */
For USR: sizeof (struct userinfo) * 100/* 100 userinfo */
Is this code easy to read? It looks like you know what it means.
In addition, sizeof is generally used to allocate memory. This feature is especially effective in multi-dimensional arrays. For example, allocate memory to a string array,
/*
* Allocate a string containing 20 characters,
* Memory of 100 characters per string
*/
Char ** P;
/*
* Incorrect allocation method
*/
P = (char **) calloc (20*100, sizeof (char ));
/*
* Correct allocation method
*/
P = (char **) calloc (20, sizeof (char *));
For (I = 0; I <20; I ++ ){
/* P = (char *) calloc (100, sizeof (char ));*/
P [I] = (char *) calloc (100, sizeof (char ));
}
(Note: The above statements are commented out in the original format and are incorrect. Correct them by dasherest. Thank you)
For the sake of easy-to-read Code, some judgments are saved. Note that these two allocation methods are essentially different.
31. Do not ignore warning
----------
Do not ignore warning information during compilation. Although these warnings do not impede the generation of the target code, they do not mean that your program is good. It is true that a program is not compiled successfully. Successful compilation is only the first step in the long journey, and there are great waves waiting for you. Starting from program compilation, not only must each error be corrected, but also each warning be corrected. This is what a well-developed programmer should do.
In general, some warning information on one side is common:
1) declared unused variables. (Although the compiler does not compile such a variable, comment it out or delete it from the source program)
2) uses a function that is implicitly declared. (This warning may appear during compilation of this function in other C files. You should declare this function using the extern keyword before)
3) no pointer is converted. (For example, if the pointer returned by malloc is void and you didn't convert it to your actual type, you should manually convert it to another one)
4) type conversion down. (For example: Float f = 2.0; this statement will warn you that the compilation will tell you that you are trying to convert a double to float. You are caressing a variable. Do you really want to do this? Add F next to 2.0. Otherwise, 2.0 is a double instead of a float)
In any case, the compiler's warning should not be underestimated. It is best not to ignore it. A single program has been made, not to mention a few small warning tasks?
32. Program for debugging and release
----------------
The program must have debugging information added by many programmers during development. I have seen many project teams. When the program development is over, why should people start to delete debugging information in the program? Why is the target code of two versions different from that of VC ++? One is the debug version and the other is the release version. The debugging information is so valuable and valuable in the future maintenance process. How can I delete the debugging information?
Use the pre-compilation technology to declare the debugging function as follows:
# Ifdef debug
Void trace (char * FMT ,...)
{
......
}
# Else
# Define trace (char * FMT ,...)
# Endif
Therefore, to allow all programs to output debugging information using trace, you only need to add a parameter "-ddebug" during compilation, such:
CC-ddebug-O target. c
Therefore, the pre-compiler will use the trace function if it finds that the debug variable is defined. If you want to release it to the user, you only need to cancel the "-ddebug" parameter, so all the macros that use the trace macro have nothing to do with this macro, therefore, all trace languages in the source program are replaced with null. How happy is it?
By the way, two very useful system macros, one is "_ file _" and the other is "_ line _", which respectively indicate the source file and row number, when debugging information or outputting errors, you can use these two macros so that you can see at a glance the first line of the file where your errors occur. This is very useful for large projects that use C/C ++.
In summary, the 32 articles are for three purposes --
1. Ease of coding.
2. maintainability of program code,
3. stable and reliable program code.
Well-developed programmers should learn to write such code! This is a small problem that any programmer must face. A programmer must not only have a strong technical skills, but also have a good foundation. The most important thing is to be "self-developed "!
Good software products are more than just technology, but more about the ease of maintenance and reliability of the entire software.
Software maintenance involves a lot of work on code maintenance, software upgrade, and a lot of work on code organization, so good code, clear and easy-to-read code, the maintenance and upgrade costs of the software will be greatly reduced.
<-Previous Page
(All Rights Reserved. Please indicate the source and author information when reprinting)