For beginners, there are the following situations:
A. The character type defined is different from that of the input function
B. The scanf function is not signed with "&"
C. The calculated number is not assigned
D. When dividing an int number, the divisor is greater than the dividend
Let's explain them one by one:
Introduce some simple calculations for circle and ball:
#include <stdio.h> #include <math.h> int main() { double r,h,pi,a1;//double entered pi = 3.1415926; printf("enter the r:"); scanf("%f",&r); printf("enter the h:"); scanf("%f",&h); a1=2.0*pi*r; printf("C circular=%6.2f\n",a1); printf("S circular=%6.2f\n",pi*r*r); printf("C ball=%6.2f\n",4*pi*r*r); printf("S ball=%6.2f\n",4/3*pi*r*r*r); return 0; }
A. This code seems to have no problem:
However, r and h defined by double must correspond to% lf, otherwise it will not be saved.
Here 1, you can change double to float.
2, you can change% f to% lf
Note: when defining with float, you can enter either% lf or% f
What% (,) is used for printf output is not so particular, just according to the requirements.
B. In blind typing, for beginners (at least I'm so hehe), "&" is easy to forget:
#include <stdio.h> int main() { int d; scanf("%d",d); printf("%d",d); return 0; }
No matter what you input, you can't output it in the end.
C. The calculated number is not assigned,
That is to say, if you write the output statement first and then the input statement, the result will be randomly assigned 0
#include <stdio.h> int main() { double d,e; double f,g; e=g;//Why e,d=0? d=f;//The problem of timeliness is that f and g have not been assigned when E and D are assigned, so it is assumed that 0 f=3157.890121; g=0.123456789; printf("e=%lf,d=%lf\n",e,d); printf("f=%lf,g=%lf",f,g); }
D. When dividing an int number, the divisor is greater than the dividend:
There are the following examples:
#include <stdio.h> int main() { int a1,b1; float a2,b2; a1=3/4; b1=3/4; a2=3.0/4.0; b2=3.0/4.0; printf("a1=%d\n",a1); printf("b1=%lf\n",b1); printf("a2=%d\n",a2); printf("b2=%lf\n",b2); printf("%d\n",3/4); printf("%lf\n",3/4); printf("%d\n",3.0/4.0); printf("%lf",3.0/4.0); return 0; }
After a simple observation, it is found that the output result is related to both the definition type and the output type:
First, if you want to output 0.75
Then the output cannot be% d, otherwise it will be calculated as 0 because the decimal cannot be displayed
Secondly, it is defined that int (integer) cannot be used. When integer calculation is performed, it is 0 regardless of the output
The conclusion is,
If 1 is written separately, it is defined as double or float
2. Floating point type is used for calculation
3. Output with% f or% lf
Compound statements are: printf ('% lf', 3.0 / 4.0); Just write
The error scenario may be like this
#include <stdio.h> int main(){ const double PI=3.14; double r,s1,s2; printf("enter r:"); scanf("%lf",&r); s1=3/4*PI*r*r*r; s2=3.0/4.0*PI*r*r*r; printf("%.21f\n",s1); printf("%.21f",s2); return 0;
As summarized above, s1 output is 0.00
If you correct the error, just like s2, change the integer number to floating point
When looking for errors, you can see whether the numerator and denominator of "/" is the required type
What does it mean to add a decimal point after a number
double a = 3/4; double b = 3.0/4.0;
The former is calculated as an integer: a=0.000000;
The latter one is calculated by floating-point type: result b=0.750000; (all output in% lf)
(floating point is simply data with decimals)
The author has just studied C language for more than a month. You are welcome to correct any deficiencies
Operating system: Windows10
Software: Devc++
Compiler: tdm-gcc 4.9.2 64 bit release