Elementary C language - detailed operator (1)

Hello, we meet again. I wonder if you have been outputting code recently? Has the ability to code improved? Let's learn operators together today. Operators are very important in our language learning. We need to know the function and usage of each operator. Let's start our study.

  1. Operator classification:

Arithmetic Operators
Shift Operators
Bitwise Operators Assignment
Operators
Unary Operators
Relational Operators
Logical Operators
Conditional Operators Comma
Expressions
Subscript References, Function Calls, and Structure Members

2 arithmetic operators

     +    -   *   /     %

Addition (+) subtraction (-) multiplication (*) is the same as mathematics, and I will tell you about the usage of division (/) and modulus (%).

#include<stdio.h>
int main()
{
    
    
	float a = 5 / 2;
	printf("%f", a);
	return 0;
}

insert image description here
We can see that although it is a floating-point type, the result is not what we want. Why is this?

The reason is that when we require the digits behind the decimal point, 5 and 2 must have a decimal, such as 5.0 or 2.0, let us add it and see the result

#include<stdio.h>
int main()
{
    
    
	float a = 5 .0/ 2;
	printf("%f", a);
	return 0;
}

insert image description here
and our modulo notation

First, the left and right sides of the modulo symbol must be integers, and the result is the remainder

#include<stdio.h>
int main()
{
    
    
	int a = 9 % 2;
	printf("%d", a);
	return 0;
}

That's right

1. In addition to the % operator, several other operators can act on integers and floating-point numbers.
2. For the / operator, if both operands are integers, integer division is performed. And as long as there are floating-point numbers, floating-point number division is performed.
3. The two operands of the % operator must be integers. Returns the remainder after division.

3. Shift operator

When talking about shift operators, we have to understand one thing first, that is binary, because our shift operators operate on two's complement

Our integers can be expressed in binary. There are three forms of binary representation of integers, which are original
code ,
inverse code, and complement code. The inverse code is that the sign bit of the original code remains unchanged, and the rest are reversed bit by bit, and the complement code is the inverse code plus 1


Here are a few examples to give you a better understanding of binary

You may not understand the calculation of binary, but you must know the decimal system. The decimal system is a commonly used number in our life. When it is full of decimals, it is like 9 to 10. Then our binary is the same. When it is full of 2, it will move forward. One bit, for example, 111 is 7 in decimal,
insert image description here
and the calculation result is our commonly used decimal

We have a simple understanding of the two-level system, so let's take a look at the complement of the original code. The complement
insert image description here
of the original code of positive integers is the same. For example, the original code of 5 is

0000000000000000000000000000101 original code
00000000000000000000000000000101 inverse code
00000000000000000000000000000101 complement code

And our shift operator is the left shift operator << that operates on two's complement

#include<stdio.h>
int main()
{
    
    
	int a = 5<<1;

	printf("%d", a);
	return 0;
}

insert image description here
insert image description here

Learn the left shift operator for positive integers, let's learn about negative numbers

#include<stdio.h>
int main()
{
    
    
	int a = -5 << 1;

	printf("%d", a);
	return 0;
}

insert image description here
Everyone must understand this explanation, so the rule of shifting to the left is to discard the right side and add 0 to the left side.
The right shift operator >>
the right operator is similar to the left shift

  1. Logical shift
    fills with 0 on the left and discards on the right
  2. Arithmetic shift
    The left is filled with the sign bit of the original value, and the right is discarded

Here's an example

#include<stdio.h>
int main()
{
    
    
	int a = 5 >> 1;

	printf("%d", a);
	return 0;
}

insert image description here
The right side of the arithmetic shift is the sign bit

Supplement: The complement code becomes the inverse code, and it can also be reversed first and then added 1, and the sign bit remains unchanged

Warning: For shift operators, do not shift negative bits, this is undefined by the standard.

int num = 10;
num>>-1;//error

4. Bitwise operators

& //按位与
| //按位或
^ //按位异或

First of all, they operate on integers

  • bitwise AND&
#include<stdio.h>
int main()
{
    
    
	int a = 5 & 3;

	printf("%d", a);
	return 0;
}

insert image description here
The bitwise AND here can be understood in this way. First, we need to write out their binary bits, just like the above and then align them. If there is one 0, the result is 0, and all 1s are 1.

  • bitwise or |
#include<stdio.h>
int main()
{
    
    
	int a = -1|3;

	printf("%d", a);
	return 0;
}

insert image description here

So a bitwise OR with a 1 is a 1

  • bitwise XOR
#include<stdio.h>
int main()
{
    
    
	int a = -1^3;

	printf("%d", a);
	return 0;
}

insert image description here

In this way, we have explained the bitwise operators clearly. Let me add that these operators are all binocular operators. What is a binocular operator? For example, 3&5, the operands on both sides of & are 3 and 5 respectively, then & It is a binocular operator. When you see &, do you think of taking the address symbol? Of course, they must be different. Taking the address & is a unary operator.

The following is a written test question, you cannot create a temporary variable (the third temporary variable) to exchange numbers

#include<stdio.h>
int main()

{
    
    
	int a = 20;
	int b = 10;
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
	printf("%d %d", a, b);
	return 0;
}

5. Assignment operator

int weight = 120;//体重
weight = 89;//不满意就赋值
double salary = 10000.0;
salary = 20000.0;//使用赋值操作符赋值。
赋值操作符可以连续使用,比如:
int a = 10;
int x = 0;
int y = 20;
a = x = y+1;//连续赋值
这样的代码感觉怎么样?
那同样的语义,你看看:
x = y+1;
a = x;
这样的写法是不是更加清晰爽朗而且易于调试。

So when we assign values ​​​​to our variables, we must at least make people understand. a = x = y+1Like this code, first assign y+1 to x and then assign to y. This kind of readability does not separate the assignment and it seems clear and complex
. Assignment operator
insert image description here
6. Unary operator
insert image description here

logical inversion

It is equivalent to turning true into false, and turning false into true. For example, if (1) in my if function is true, but if we add it! It becomes if (!1) is logical false

Negative and positive values
, insert a code and everyone will understand immediately

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

The answer displayed by the compiler is that
the positive value of 4 is the same as our positive number

  • .Get Address&

is to take out the location of the variable in our memory

  • sizeof
#include <stdio.h>
int main()
{
    
    
 int a = -10;
 int *p = NULL;//定义为空指针
 printf("%d\n", !2);
 printf("%d\n", !0);
 a = -a;
 p = &a;
 printf("%d\n", sizeof(a));
 printf("%d\n", sizeof(int));
 printf("%d\n", sizeof a);//这样写行不行?可以
 printf("%d\n", sizeof int);//这样写行不行?答案是不能
 return 0;
}

The byte size of the address under the X86 compiler is 4 bits, and under the 64-bit platform, the byte size is 8 bits

The most difficult thing in sizeof is to combine with arrays, because you may always have a misunderstanding, let me give you an example.

#include <stdio.h>
void test1(int arr[])
{
    
    
	printf("%d\n", sizeof(arr));//(2)
}
void test2(char ch[])
{
    
    
	printf("%d\n", sizeof(ch));//(4)
}
int main()
{
    
    
	int arr[10] = {
    
     0 };
	char ch[10] = {
    
     0 };
	printf("%d\n", sizeof(arr));//(1)
	printf("%d\n", sizeof(ch));//(3)
	test1(arr);
	test2(ch);
	return 0;
}
//问:
//(1)、(2)两个地方分别输出多少?
//(3)、(4)两个地方分别输出多少?

insert image description here
As long as you know the address of the first element passed by the function when passing the array, I believe you should be able to understand it. The previous article also specifically talked about it. If you don’t know it, just look forward.

  • ~
    is a bitwise inversion, which is equivalent to turning the 1 part of a binary number into 0, and the 0 part into 1
#include<stdio.h>
int main()
{
    
    
	int a = 2;
	printf("%d", ~a);
	return 0;
}

insert image description here

- Prepend ++ and –
before ++ or – and then use

#include<stdio.h>
int main()
{
    
    

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

	int a = 1;
	int b = --a;//a=a-1 b=a
	printf("%d %d", a, b);
	return 0;
}

postfix ++ and –


#include<stdio.h>
int main()
{
    
    

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

	int a = 1;
	int b = a--;//b=a a=a-1
	printf("%d %d", a, b);
	return 0;
}

dereference operator *

It is generally used in pointers. For example, when we want to know the content of a pointer variable, we can use * to know its content. Can you find that it is just the opposite of & to get the address?

() This symbol is mandatory conversion, for a simple example


#include<stdio.h>
int main()
{
    
    
	float a = 5.0;
	int c = (int)a / 2;
	printf("%d", c);
	return 0;
}

Although it is a floating-point type, after our coercion, the last a is an integer

  • 7. Relational Operators
    insert image description here

Warning: Be sure to distinguish between == and =.
That’s all for today’s sharing, and I look forward to seeing you next time

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/131574132