Since the C + +, which was directly learned from C + +, though C + +, is somewhat different, I simply start with the simplest:
Program 1-1 calculates and outputs a value of 1 + 2
1 #include <stdio.h>2int main ()3{4 printf ( " %d \ n " 1 2 ); 5 return 0 ; 6 }
There is no difficulty,%d is a placeholder,
When compiling, the following values are brought in, where D is the decimal.
Extended experiment: Change 1+2 to 8/5
You can see that the output is 1, not 1.6, for the same reason as C + +: 8/5 The exact meaning is 8 divided by the integer portion of 5.
To get 1.6 is also easy.
Program 1-2 calculates and outputs a value of 8/5 and retains 1 digits after the decimal point
1 #include <stdio.h>int main (){4 printf ("%.1f \8.0/5); change the original%d to%.1f0; 7}
Note: The percent symbol follows a decimal point followed by the number 1, which represents the exact 1 digits after the decimal, and finally the lowercase f.
8 instead of 8.0, the reason is also simple in the world of computers integer-to-integer operations result in integers, and when a floating-point number is calculated, the result is a floating-point number.
Summarize:
1. Integer output with%d, floating point output with%f.
2. Integer/integer = integer; floating-point/float = floating-point number
Program 1-3 calculation of complex expressions
1#include <stdio.h>2#include <math.h>//******************************3 intMain ()4 {5printf"%.8f \ n",1+2* SQRT (3) / (5-0.1));6 7 return 0;8}
There's nothing to note here, just know that when you encounter a mathematical function in your program, you have to include MATH.H in your header file
1.1 Arithmetic expressions