Chapter 3: When the boss of the good machine
Whenever we don't forget that we are learning a language, and the basic requirements for learning the language are: accurate use of it to express their intentions, not only let the machine read, but also let others (as long as he will c Language) Read what you mean. Remember, the language is used to communicate, whether it is a programming language or a natural language. Now let's make an analysis of these two exchanges, how can you let them understand what you want, how to do it.
For machines, we have to do more simple points. The grammar of programming languages is much simpler than natural language. Everything is complicated in order, choice, and cycles. The beginners must do it. The process of "copy-rewriting - imitation - habits". Waiting for these statements to become your habits, it is great, just like you speak Chinese, you will not consider the statement or an exclamation sentence, huh, (this reminds me of my bad English, sweat ~~ ~). Of course, we are far more than that of the machine, let the machine read this only first step, how to make the machine to run better according to our meaning, faster is the realm of our pursuit, of course, this realm is not end. Slowly accumulate in experience, just put forward a few individual recommendations:
the first. Try to use local variables. Because the C language has a feature, variables in the same domain must be defined before all processing statements (except for division [O1]), which means that all static space must be assigned when the program starts, and many data It is very small in the program, so we need to reduce these unnecessary overhead, flexible use of divided programs to further locate these objects, compare the following two codes:
Code1:
#include "stdio.h"
#include "stdlib.h"
int main ()
{
IX;
Char C;
Scanf ("% C", & C);
IF (c == 'y')
{
IX = 100;
Printf ("this IS% D! / N", IX);
}
System ("pause");
Return 0;
}
Code2
#include "stdio.h"
#include "stdlib.h"
int main ()
{
Char C;
Scanf ("% C", & C);
IF (c == 'y')
{
INT IX = 100;
Printf ("this IS% D! / N", IX);
}
System ("pause");
Return 0;
}
You will find that if we don't enter the 'y' system, it is not necessary to assign space for IX.
Second, pay attention to the language characteristics like bugs, such as the Switch statement, can say that the controversy from the C language is not stopped, and it brings us a lot of trouble. . Sally, in the second chapter of "C expert program", it is "more doing", but we find that it is sometimes it or does not replace, such as judging whether a number belongs to a discrete collection:
#include "stdio.h"
#include "stdlib.h"
int main ()
{
INT I;
While (Scanf ("% D", & I)! = EOF)
{
Switch (i)
{
Case 1: Case 2:
Case 3: Case 5:
Case 8: Case 13:
Printf ("yes! / n");
Break;
DEFAULT:
Printf ("NO! / N");
Break;
}
}
System ("pause");
Return 0;
}
Oh, this series is familiar with, but in addition to the Switch statement, can you find a more concise expression than him? But this is exactly using the downward deviation of the statement. The goto statement also has a similar situation, as long as we carefully study, these seems very troublesome things will become very beautiful.
Ok, let's talk about it. In the next note, we will talk to people's communication - the style of the program.
[O1] is also called a composite statement
(This article is first set to the second is the bookstore)