C language - operators and expressions

Knowledge point one: Expressions

          1) What is an expression: In C language, one or more operands and zero or more operators form an expression;

example:

100
5 + 10
a / b
a * 10 / b + c

           2) The result of the expression:

printf("%d\n", 5 + 10);

The operation between an operator and its operand must produce a result, so every expression has a result . For example, the expression 5 + 10 above evaluates to 15.

            3) Expression statement:

                  a) Add a semicolon after the expression to form an expression statement;

                  b) Expressions in C language cannot exist alone and must exist in the form of expression statements;

example:

100;
5 + 10;
a / b;
a * 10 / b + c;

"But there is no semicolon after the following expression 5+10"

printf("%d\n", 5 + 10);

#Because 5 + 10 is a subexpression, the function name plus the bracket operator forms a function call expression. Therefore, 5 + 10 is a subexpression of the function call expression, and a semicolon is added after the function call expression.

#The function call expression also has an expression result, which is the return value of the function.

Knowledge point 2: Addition operator +

          1) + is the addition operator, which requires an operand on its left and right sides to form an addition operator expression;

          2) Expression result : The result of the addition expression is the addition of the left and right expression results ;

int a, b;
a = 200;
b = 100;
a + b; // 表达式结果为300

Knowledge point 3: Subtraction operator—

          1) - is a subtraction operator, which requires an operand on its left and right sides to form a subtraction operator expression;

           2) Expression result : The result of the subtraction expression is the subtraction of the left and right expression results ;

int a, b;
a = 200;
b = 100;
a - b; // 表达式结果为100

Knowledge point four: symbolic operators +, -

           1) + is used to indicate the sign of an expression, - is used to change the sign of an expression;

           2) Unlike the addition and subtraction operators, it only requires one operand on the right side of the operator;

           3) Expression result:

                  a) The result of the + expression is the value of the operand on the right;

                  b) The result of the - expression is the opposite of the operand value on the right;

+100; // 表达式结果为100
-100; // 表达式结果为-10

             4) Used to change the sign of an expression:

int a = 100;
-a; // 表达式结果为-100

            5) Please note: + cannot change the sign of an expression, please use negative to get positive;

int a = -100; 
+a; // 表达式结果为-100 
-a; // 表达式结果为100

Knowledge point 5: Multiplication operator *

          1) * is a multiplication operator, and its left and right sides each require an operand to form a multiplication operator expression;

          2) Expression result: The result of the multiplication expression is the multiplication of the left and right expression results;

          3) * is the multiplication operator, be careful not to misuse it with the letter x;

int a, b;
a = 200;
b = 100;
a * b; // 表达式结果为20000

Knowledge point 6: Division operator /

           1) / is the division operator, which requires an operand on its left and right sides to form a division operator expression;

           2) Expression result : The result of the division expression is the left operand divided by the right operand;

int a, b;
a = 200;
b = 100;
a / b; // 表达式结果为2

           3) Problem that integers cannot be divided:

                 a) For division, please pay special attention to the problem of floating-point data when the integer cannot be divided;

int a, b, c;
a = 5;
b = 2;
c = a / b;

# The value of a is 5 and the value of b is 2. So 5 divided by 2 should equal 2.5. However, it seems wrong to use int to receive 2.5, so let's change c to float or double.

int a, b;
a = 5;
b = 2;
float c;
c = a / b;
printf("%f\n", c);

 #Obviously the result is still wrong

#In C language, the result of integer and integer operations is still an integer . The fractional part of the result is discarded, a process called truncation . After a / b, the result is still an integer and the value has been truncated. At this time, we assign it to a floating point type c. It can only be converted from integer 2 to floating point 2.0, but it has no effect.

& Then we change a, b, c to float, so that there will be no truncation. a, b will perform floating point operations, and the result will also be a floating point number type float;

float a, b, c;
a = 5;
b = 2;
c = a / b;
printf("%f\n", c);

 Knowledge point 7: Remainder operator %

            1) % is the remainder operator, which requires an operand on its left and right sides to form a remainder operator expression;

            2) Expression result : The result of the remainder expression is the remainder of the left operand divided by the right operand;

For example: 10%3, 10 divided by 3, equals 3 with a remainder of 1

int a, b;
a = 10;
b = 3;
a % b; // 表达式结果为1

Knowledge point 8: Assignment operator =

           1) = is the assignment operator, which requires an operand on the left and right sides to form an assignment operator expression;

            2) The result of the assignment expression is the value of the operand on the right side of the equal sign;

            3) The assignment expression will also transfer the value of the operand on the right side of the equal sign to the operand on the left;

a = 100; // 赋值表达式语句

            4) The difference between assignment and initialization:

int a = 100; // =表示初始化,不是赋值运算符
a = a + 150; // 赋值运算符,将右边的表达式结果赋值给左边的变量a。

In the above code, the first line of code declares a variable a. Please note that the = sign used when declaring the variable and initializing it to 100 is not an assignment operator, but a variable initialization. Although initialization is very similar to assignment, the left side of the = sign is not a simple variable , but a declaration of the variable . You can use this to distinguish them.

             5) Find the result of an assignment expression:

The expression a = a + 50 is a compound expression. First, the result of the subexpression a + 50 is obtained, which is 150. Then find the result of the expression a = 150, which is 150. In addition, in addition to calculating the result of the expression, the assignment expression also assigns 150 to the variable a

              6) You cannot assign a value to a constant: It is wrong to assign a value to a constant because a constant cannot be changed;

"Hello" = "HelloWorld";
'a' = 'b';
100 = 200;

"The above assignment is the wrong way"

Knowledge point 9: Auto-increment and auto-decrement operators

          1) ++ refers to the increment operator, and -- refers to the decrement operator. They only require an operand to the left or right of the operator;

          2) Placing the operator on the left is called prefix mode. For example: ++i --i;

          3) Placing the operator on the right is called suffix mode. For example: i++ i--;

          4) Prefix mode:

#include <stdio.h>
int main()
{
int a, b;
a = 10;
b = 10;
printf("%d %d\n", ++a, --b);
printf("%d %d\n", a, b);
return 0;
}

              a) Prefix pattern expression result:

                    ++ and the right operand form a prefix auto-increment expression, and the result of the expression is the value of the operand plus 1;

                    - - forms a prefix decrement expression with the right operand, and the result of the expression is the value of the operand minus 1;

              b) Prefix mode operations on operands:

                   Autoincrement expression: add 1 to the operand;

                   Decrement expression: decrement the operand by 1;

       5) Suffix mode:

#include <stdio.h>
int main()
{
int a, b;
a = 10;
b = 10;
printf("%d %d\n", a++, b--);
printf("%d %d\n", a, b);
return 0;

           a) Postfix pattern expression result:

                ++ and the left operand form a suffix auto-increment expression, and the result of the expression is the value of the operand;

                -- forms a suffix decrement expression with the left operand, and the result of the expression is the operand value;

          b) Postfix mode operations on operands:

               Autoincrement expression: add 1 to the operand;

               Decrement expression: decrement the operand by 1;

Knowledge point ten: unary operators, binary operators

           1) Unary operator: An operator with only one operand. For example: increment operator ++, decrement operator --;

           2) Binary operator: An operator with two operands. For example: assignment operator =, addition operator +;

Knowledge Point 11: Operator Priority

* The precedence of the four arithmetic operators in C language is consistent with that in mathematics. Calculate what is inside the brackets first, multiply and divide first, then add and subtract;

#include <stdio.h>
int main()
{
int a, b;
a = 10 * 2 + 4 * 3;
b = 10 * (2 + 4) * 3;
printf("%d %d", a, b);
return 0;
}

 

 

Common operator precedence
mark Operator type priority
++  -- self-increasing, self-decreasing prefix 16
++  -- self-increasing, self-decreasing suffix 15
+  - Positive sign, negative sign Monocular 15
* / % Multiply, divide, remainder twin eyes 13
+ - Add, subtract twin eyes 12
= Assignment twin eyes 2

The higher the priority, the higher the priority in the operation. For example: the priority of multiplication and division is 13, which is greater than the priority of addition and subtraction of 12.

Guess you like

Origin blog.csdn.net/m0_73463638/article/details/127190794