My C Language Learning 1, C Language Learning 1
Learning is happy, especially when I saw a program confused and understood what was going on. This process is very exciting and makes me keep moving forward and cannot extricate myself, today is coming to an end.
1.1-first C Language
# Include <stdio. h>
Main ()
{
Printf ("hello, world \ n ");
}
Your mistakes :";"Sometimes, you may forget to pay attention to this.
Tip: \ n line break
1.2-variables and arithmetic expressions (Direct Calculation of Fahrenheit temperature and Celsius temperature)
Whlie
# Include <stdio. h>
Main ()
{
Int fahr, celsius;
Int lower, upper, step;
Lower = 0;
Upper = 300;
Step = 20;
Fahr = lower;
While (fahr <= upper ){
Celsius = (5 * (fahr-32)/9;
Printf ("% 1d \ t % 6d \ n", fahr, celsius );
Fahr = fahr + step;
}
}
Your own problem: when you don't have any tips, you may find that some parts are not remembered or the order is mixed. Later, you may think about logic and some basic things. For example, fahr, celslius, lower, upper, and step should be placed at the beginning, because all variables must be declared and used first, usually at the beginning of the function.
Tip: 1. int indicates that the input is an integer, And the float behind it is a decimal floating.
2, because it is int so celsius = (5 * (fahr-32)/9, if it is float can make celsius = (5.0/9.0) * (fahr-32)
In C language, the integer division operation is performed in a given place. If the decimal number is 5/9, the reserved integer is discarded. The calculated result leads to a large deviation. When it is float, the floating point division is, the result will not be given away.
1.3for statement
For
# Include <stdio. h>
Main ()
{
Int fahr;
For (fahr = 0; fahr <= 300; fahr = fahr + 20)
Printf ("% 3d \ t % 6.1f \ n", fahr, (5.0/9.0) * (fahr-32 ));
}
Repeat the old story:; this is really important. It may be that when I am a newbie, I will easily forget it, which leads to program errors, but will not prompt you because I didn't; Because; if the int fahr parameter is missing, the int fahr parameter is invalid, that is, the variable fahr is not declared first. Therefore, the program is incorrect.
1.4 symbolic Constants
Symbol constant define (Replace text)
# Include <stdio. h>
# Define LOWER 0
# Define UPPER 300
# Define STEP 20
Main ()
{
Int fahr;
For (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
Printf ("% 3d \ t % 6.1f \ n", fahr, (5.0/9.0) * (fahr-32 ));
}
Tip; simply put, define is a replacement function. It can replace any symbol and is not limited to numbers. It is good to pay attention to the case when using it.
Finally, I would like to ask if I agree that the temperature is calculated. What are the advantages and disadvantages of the first while, the second for, and the third define?
In my opinion, while is the most rigorous and for is the simplest. define is not quite clear, but these views may be wrong. Wait for your opinion
If you want to correct your mistakes or have some opinions, you can say that if you have a learning partner, there will be many questions in the next article, but it may be naive. (Attach My QQ: 3079269453)