Introduction to Arithmetic, Assignment, and Unary Operators

arithmetic operator

Arithmetic operators include the following symbols:
1: + (plus sign)
2: - (minus sign)
3: * (multiplication sign)
4: / (division sign)
5: % (remainder sign
) C language operation plays an important role, let's introduce it below

1.1: + and -

The functions of + and - are the same as those we have learned in mathematics. They are used to complete addition and subtraction, such as the following code:

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

The final print results of a and b here are 5 and -1 respectively.

1.2:*

*It has the same meaning as × in mathematics, and the algorithm is also the same.

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

We make a simple modification to the previous code. Through calculation, we get the value of c to be -5, and the final running result is also -5.

1.3:/

/ is to divide the formula.

#include<stdio.h>
int main()
{
    
    
	float a = 6 / 4;
	int b = 6 / 4;
	printf("%f\n", a); //输出 1.000000
	printf("%d\n", b); //输出 1
	return 0;
}

Why is the output not 1.5?
In fact, there is a rule: if the two ends of the division sign are integers, integer division is performed, and the result is also an integer.
Although the type of the variable x is float (floating point number), the result of 6 / 4 is 1.0, and not 1.5. The reason is that the integer division in the C language is an integer division, and only the integer part is returned, and the fractional part is discarded.

If we want to get the few digits after the fractional part, we can make at least one of the operands a floating point number.

#include <stdio.h>
int main()
{
    
    
 float x = 6.0 / 4; // 或者写成 6 / 4.0
 printf("%f\n", x); // 输出 1.500000
 return 0;
}

6.0 / 4 means to perform floating-point division, and the result is 1.5
Let's think about another situation

include <stdio.h>
int main()
{
    
    
 int score = 5;
 score = (score / 20) * 100;
 return 0;
}

In the above code, you may think that after calculation, score will be equal to 25, but actually score is equal to 0. This is because
score / 20 is divisible and will result in an integer value of 0, so multiplying by 100 will result in 0 as well.
In order to get the expected result, you can change the divisor 20 to 20.0, so that integer division becomes floating-point number division.

include <stdio.h>
int main()
{
    
    
 int score = 5;
 score = (score / 20.0) * 100;
 return 0;
}

1.4:%

% is a modulo operation in mathematics, but in C language it returns the remainder of the division of two integers. This operator can only be used on integers, not floating point numbers.

#include <stdio.h>
int main()
{
    
    
 int x = 6 % 4; // 2
 return 0;
}

Let's look at another situation:

#include <stdio.h>
int main()
{
    
    
 printf("%d\n", 11 % -5); //结果为 1
 printf("%d\n",-11 % -5); //结果为-1
 printf("%d\n",-11 % 5);  //结果为-1
 return 0;
}

This result is different from the result of our usual mathematical operations.
There is a rule in the modulo of negative numbers: the sign of the result is determined by the sign of the first operand.

assignment operator

Giving an initial value when a variable is created is called initialization, and giving a value after the variable is created is called assignment

int a = 100;//初始化
a = 200;//赋值,这⾥使⽤的就是赋值操作符

The assignment operator = is an operator that can assign a value to a variable at any time.

2.1: Continuous assignment

Assignment can be continuous, for example:

int a = 3;
int b = 5;
int c = 0;
c = b = a+3;//连续赋值,从右向左依次赋值的。

This code is also equivalent to

int a = 3;
int b = 5;
int c = 0;
b = a+3;
c = b;

But I personally recommend the second code, because it is easier to understand and observe.

2.2: Compound assignment operator

When writing code, we may often perform self-increment and self-decrement operations on a number, as shown in the following code:

int a = 10;
a = a+3;
a = a-2;

This code is also equivalent to

int a = 10;
a += 3;
a -= 2;

Therefore, we can know from these two codes
that a=a+3 can be written as a+=3
C language provides compound assignment symbols, which is convenient for us to write codes. These assignment symbols are

+=  -=
*=  /=  %=
>>=  <<=
&=  |=  ^=

unary operator

The operators described above are all binary operators. There are also some operators in C language that have only one operand, which are called unary operators.
That is ++, –, +, -.

3.1++ and –

++ is a self-increment operator, which is also divided into pre-++ and post-++, – is a self-decrement operator, and is also divided into pre-– and post-–

3.1.1: Prefix ++ and Prefix –

Front ++ (+1 first, then use):

int a = 10;
int b = ++a;//++的操作数是a,是放在a的前⾯的,就是前置++
printf("a=%d b=%d\n",a , b);
1

Let's look at another example:

int a = 10;
a = a+1;
b = a;
printf("a=%d b=%d\n",a , b);

The process is: a is originally 10, first +1, then a becomes 11, and then it is assigned to b, and b gets 11

Prepend – (-1 first, then use):

int a = 10;
int b = --a;//--的操作数是a,是放在a的前⾯的,就是前置--
printf("a=%d b=%d\n",a , b);//输出的结果是:9 9

3.1.2: post ++ and post –

Back ++ (use first, then +1):

int a = 10;
int b = a++;//++的操作数是a,是放在a的后⾯的,就是后置++
printf("a=%d b=%d\n",a , b);

Let's look at another example:

int a = 10;
int b = a;
a = a+1;
printf("a=%d b=%d\n",a , b);

The process is: a is originally 10, and it is used first, that is, it is first assigned to b, b gets 10, then +1, and then a becomes 11.

postfix – (use first, then -1):

int a = 10;
int b = a--;//--的操作数是a,是放在a的后⾯的,就是后置--
printf("a=%d b=%d\n",a , b);//输出的结果是:9 1

3.2: + and -

Here + is a positive sign, - is a negative sign, both are unary operators.
Operator + has no effect on positive and negative values. It is an operator that can be omitted completely, but it will not report an error if it is written.

int a = +10; 等价于 int a = 10;

Operator - Used to change the sign of a value, adding - in front of a negative number will result in a positive number, adding - in front of a positive number will result in a negative number

int a = 10;
int b = -a;
int c = -10;
printf("b=%d c=%d\n", b, c);//这⾥的b和c都是-10
int a = -10;
int b = -a;
printf("b=%d\n", b); //这⾥的b是10

Guess you like

Origin blog.csdn.net/2301_79178723/article/details/131914544