Elementary C language - detailed explanation of operators (2)

hello, we meet again, today we finish the chapter on operators, let's learn together

  1. logical operator

&& logical AND
|| logical or

Here we have to distinguish between bitwise and and bitwise or, as well as logical and and logical or.

1&2----->0
1&&2---->1
1|2----->3
1||2---->1

Bitwise and and bitwise or binary calculations, and logical and logical or true or false problems, for example, if we want to judge whether a year is a leap year, the first condition that must be met is a multiple of four and cannot be divisible by 100. The two conditions are met at the same time, so we can write it like this. (x%4==0&&x%100==0)There is also a condition that needs to be met in a leap year that can be divisible by 400. Then we can write the standard of whether this year is a leap year as follows x%4==0&&x%100==0||x%400==0.

Logic and both must be true for logic to be true, and if one of logic or is true, then our logic is true

  1. conditional operator

exp1 ? exp2 : exp3

If the condition of exp1 is true, then execute exp2, if not, execute exp3, which is very similar to the if function

#include<stdio.h>
int main()
{
    
    
	int a = 10;
	int b = 20;
	int max = 0;
	if (a > b)
	{
    
    
		max = a;
	}
	else
	{
    
    
		max = b;
	}
	printf("%d", max);
	return 0;
}
#include<stdio.h>
int main()
{
    
    
	int a = 10;
	int b = 20;
	int max = a > b ? a: b;
	printf("%d", max);
	return 0;
}

Both codes work the same, but obviously the second one looks a bit simpler

  1. comma expression

exp1, exp2, exp3, expn

Comma expressions are multiple expressions separated by commas.
Comma expressions are executed sequentially from left to right. The result of the entire expression is the result of the last expression.

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

insert image description here
As long as we count in order, we can calculate the result

Comma expressions can also have the effect of optimizing code

  1. Subscript references, function calls, and structure members

  2. [ ][ ] Subscript reference
    operator Operands: an array name + an index value

int arr[10];//创建数组
 arr[9] = 10;//实用下标引用操作符。
 [ ]的两个操作数是arr和9
  1. ( ) The function call operator
    accepts one or more operands: the first operand is the function name, and the remaining operands are the parameters passed to the function.
#include <stdio.h>
 void test1()
 {
    
    
 printf("hehe\n");
 }
 void test2(const char *str)
 {
    
    
 printf("%s\n", str);
 }
 int main()
 {
    
    
 test1();            //实用()作为函数调用操作符。
 test2("hello bit.");//实用()作为函数调用操作符。
 return 0;
 }
  1. Access a member of a structure
    . Structure. Member name
    -> Structure pointer -> Member name

Let's give an example to illustrate how to use these two symbols.
We are now going to create a student's information. First, is there a name, then his age, and secondly, we are defining a gender, so we need to use the structure here The body is struct.

#include<stdio.h>
struct student
{
    
    
	char name[20];//放名字
	int age;//放年龄
	char sex[5];//放性别
};
int main()
{
    
    
	struct student s1 = {
    
     "张三",18,"男" };
	printf("%s %d %s", s1.name, s1.age, s1.sex);
	return 0;
}

insert image description here
This is our use of. Next we are looking at how to use the same code -> to use

#include<stdio.h>
struct student
{
    
    
	char name[20];//放名字
	int age;//放年龄
	char sex[5];//放性别
};
int main()
{
    
    
	struct student s1 = {
    
     "张三",18,"男" };
	//printf("%s %d %s", s1.name, s1.age, s1.sex);
	struct student* ps1=&s1;
	printf("%s %d %s", ps1->name, ps1->age, ps1->sex);
	return 0;
}

It doesn't matter if you don't understand the structure here, I will talk about it later, and I said it before, so you can look back

  1. expression evaluation

The order in which expressions are evaluated is determined in part by the precedence and associativity of operators.
Likewise, operands of some expressions may need to be converted to other types during evaluation.

  • .1 Implicit type conversion

C's integer arithmetic operations are always performed with at least the precision of the default integral type.
To achieve this precision, character and short integer operands in expressions are converted to plain integer types before use, a conversion known as integer promotion.
Significance of integer promotion
The integer operation of the expression must be executed in the corresponding computing device of the CPU. The byte length of the operand of the integer arithmetic unit (ALU) in the CPU is
generally the byte length of int, which is also the general purpose of the CPU. The length of the register.
Therefore, even if the addition of two char types is actually performed by the CPU, it must first be converted to the standard length of the integer operand in the CPU.
It is difficult for a general-purpose CPU (general-purpose CPU) to directly add two 8-bit bytes directly (although there
may be such byte addition instructions in machine instructions). Therefore, the various integer values ​​in the expression whose length may be smaller than the length of int must be converted to int or unsigned int before being sent to the CPU to perform operations

Let's take an example

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

//Negative integer promotion
char c1 = -1;
there are only 8 bits in the binary bit (complement code) of the variable c1:
1111111
Because char is a signed char,
when the plastic promotion is performed, the high bit supplements the sign bit, which is 1
The result after boosting is:
1111111111111111111111111111111

Integer Promotions for Integers

char c2 = 1;
there are only 8 bits in the binary (complement code) of variable c2:
00000001
Because char is a signed char
, when the plastic is promoted, the high bit is supplemented with the sign bit, which is 0.
The result after promotion is:
00000000000000000000000000000001

Note that unsigned numbers only need to be filled with zeros.

2 Arithmetic conversions
If the operands of an operator are of different types, the operation cannot be performed unless one of the operands is converted to the type of the other. The hierarchy below is called ordinary arithmetic conversions.

If the type of an operand ranks lower in the above list, the operation must first be converted to the type of the other operand.
WARNING:
But arithmetic conversions need to be reasonable, otherwise there are some potential problems

float f = 3.14;
int num = f;//隐式转换,会有精度丢失

Properties of Operators
There are three factors that affect the evaluation of complex expressions.

  1. operator precedence
  2. operator associativity
  3. Whether to control the order of evaluation.

So our expression evaluation part is determined by the precedence of the operator

That's all for today's sharing, see you next time

Guess you like

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