The type of arithmetic operation result in C language is determined by the type of the two operands involved in the operation. When two ints are used for arithmetic operation, the result must also be of type int (when two integers are divided, the actual operation is an integer division operation) )
% NumberRemainder operator: Remainder operation requires that the left and right operands must be integers. The sign of the remainder operation result in C language is determined by the left operand. )
% sign and / signThe denominator of can not be zero
Relational operators
The result of a relational operator is 1 or 0 (1 represents true, 0 represents false)
Conditional operator (ternary operator)
General form:条件1 ? 条件2 :条件3
Meaning: Determine whether the result of condition 1 is true. If the result of condition 1 is true, the result of the entire conditional expression is condition 2, otherwise the result of the entire conditional expression is condition 3.
The ternary operator is generally interchangeable with if-else
a++ give++a
a++: The value of a is incremented by one after this statement ends.
++a: When this statement is executed, the value of a has been increased by one.
2. Variables
definition
Variable definition: <数据类型> <变量名> = <初值>When a variable is defined, it should be assigned an initial value.
Variable name: also called identifier, can only consist of letters, numbers, and underscores. Numbers cannot be in the first place, and C language keywords cannot be used as variable names. The identifier can be up to 32 characters long.
You cannot use continuous equality to assign values to variables, for exampleint a = b =0;
Multiple variables cannot be assigned directly after being defined, separated by commas, for examplea, b, c = 0;
const keyword
Keywords used to define read-only variables
Global variables and local variables
Scope: the scope in which the variable takes effect
Local variables: Variables defined in a function can only be used in the function. They are called local variables and their scope is within the curly brackets of the function.
Global variables: variables defined during preprocessing, which can be used globally and have a global scope.
3. Basic data types
floating point number
float type: single-precision floating point number, 4 bytes (B), 32 bits (b). Use %f
Double type: double-precision floating point number, 8 bytes, 64 bits. Use %lf
Operation
When two integers are operated on, the result is still an integer.
When integers and floating-point numbers are operated together, the integers are automatically converted into floating-point numbers, and the result is also a floating-point number.
floating point output
Rounding: When using %.digit f and %.digit lf, the data will be automatically rounded.
Manual remainder (no rounding)
intmain(){
double pi =3.1415926;//题目测试样例数字int temp =(int)(pi *pow(10,n));//希望保留n位小数
pi = temp /(pow(10,n)*1.0);//将结果处理为有小数的浮点数printf("%.nf",pi);//保留小数点后n位并输出结果return0;}