# Operators and Expressions

Operator: it refers to the symbols involved in the operation. The variables operated with the operator are called operands

Expression: it is a formula composed of variables or direct quantities and operators. The expression will have a calculation result, which is called the value of the expression

### Concepts of operators and expressions

In applications, data is often operated, so le C language provides many types of operators, that is, symbols specially used to tell programs to perform specific operations or logical operations

There are seven common categories:

Operator type | effect |
---|---|

Arithmetic operator | Used to process four operations |

Relational operator | Used to compare expressions and return a true or false value |

Logical operator | Used to return true or false values based on the value of an expression |

Assignment Operators | Used to assign the value of an expression to a variable |

Conditional operator | Used for processing condition judgment |

Bitwise Operators | Bit operation for processing data |

sizeof operator | Used to find byte length |

### Arithmetic operators and arithmetic expressions

The arithmetic operator in C language is the symbol used to deal with four operations

Let's go straight to the figure above ππππΊππ»

operator | name | Real column | Operation result |
---|---|---|---|

+ | Plus sign | +3 | 3 |

- | minus sign | a=4;-a; | -4 |

+ | plus | 1+1 | 2 |

- | minus sign | 2-1 | 1 |

* | ride | 9*9 | 81 |

/ | except | 6/3 | 2 |

% | Mod | 6%5 | 1 |

++ | Self increasing (front) | a=2;b=++a; | b=3;a=3; |

++ | Self subtraction (front) | a=2;b=- -a; | b=1;a=1; |

β | Self increasing (rear) | a=2;b=a++; | b=2;a=3; |

β | Self subtraction (rear) | a=2;b=a- - | b=2;a=1; |

With regard to self increase and self decrease, we must pay attention to:

(+ sign first) + + a indicates that a is participating in other operations after self increment

(- sign first) - A indicates that a is participating in other operations after subtraction

(+ sign after) a + + indicates that the value of a increases by 1 after participating in the operation

(- sign after) -- ab indicates that the value of a after participating in the operation is minus 1

Arithmetic expression

Expressions connected by arithmetic operators are called arithmetic expressions.

Upper Code:

#include<stdio.h> int main() { int a,b,c,x,y,z; a=1; b=2; c=3; printf("%d\n",a+b); printf("Self increment (front):%d\n" ,++a); //A increases automatically, 1 + 1 = 2, and is stored in A. at this time, the value in a has changed to 2 (no longer 1), and the output result in the operation is 2. printf("Self increasing (after):%d\n",a++); //A operates first, because the value stored in a is 2, so a=2. At this time, the operation is assigned to a=2. The output result of the operation is 2. Then it increases automatically to get a=3. Later, the result is obtained by self increment and stored in a, but it is not used printf("here a Value of:%d\n",a); //In this step, we use, which is equal to 3, so the output of the operation is a=3 printf("Self subtraction (after):%d\n",b--);//First b operation, the value assigned to a above is 2, so b=2, the output value is 2, and then self subtraction, 2-1 = 1. At this time, the value stored in b is 1 printf("Self subtraction (front):%d\n",--b);//Self subtraction first, because the value stored in b in the previous step is 1, b=1, so 1-1 = 0. Then, during operation, the value of output b is 1 printf("Self subtraction (front):%d\n",--c); printf("The value of remainder is:%d\n",7%5); } //The operation result is: 3 Self increasing (front): 2 Self increasing (rear): 2 here a Value of: 3 Self subtraction (after): 2 Self subtraction (front): 0 Self subtraction (front): 2 The value of remainder is: 2

### Relational operators and expressions

The operator that judges the relationship between two data is called relational operator, also known as comparison operator

operator | operation | The Works of Liezi | result |
---|---|---|---|

== | Equal to | 1==2 | 0 (false) |

!= | Not equal to | 1!=2 | 1 (true) |

< | less than | 1<2 | 1 (true) |

> | greater than | 1>2 | 0 (false) |

<= | Less than or equal to | 1<=2 | 1 (true) |

>= | Greater than or equal to | 1>=2 | 0 (false) |

Relational operators are binocular operators. They need two variables in operation, such as a > B

Relational expression

Expressions connected by relational operators are called relational expressions

#include<stdio.h> main() { int x,y,z; //Variable description (Declaration) //The variable declaration in the function must be placed at the top of the function body int max(int a,int b); //Function declaration (description) printf("Enter two integers,Judge size:"); //Output string scanf("%d\n %d",&x,&y); //Enter values for x and y, //scanf() is a built-in input function z=max(x,y); //First call the max() function, //Then assign the value returned by the max() function to z printf("The maximum number is: %d \n",z); //Output string //printf() is a built-in output function } //Main function (main) int max(int a,int b) { if (a>b) return a;else return b; } //Custom max function

### Logical operators and logical expressions

Logical operators are used to judge whether a compound condition is true or false, and the result is still (true) or (false)

operator | name | The Works of Liezi | result |
---|---|---|---|

&& | And | a&&b | If both a and b are true, the result is true, otherwise it is false |

! | wrong | !a | If a is false,! A is true, otherwise it is false |

II | or | aIIb | True if one or more of a and b are true, otherwise false |

Logical expression

Expressions connected by logical operators are called logical expressions

The code was seen by the blogger Xinyuan's wife

#include<stdio.h> int main() { int age=0; // Age 18-30 int height=0; // Height 165-175 cm int sc=0; // Figure, 1-hot; 2-ordinary; 3-airport. int yz=0; // Appearance, 1-beautiful; 2-average; 3-crooked melon split jujube (Cuihua). printf("Please enter age, height and figure(1-Hot; 2-Ordinary; 3-airport)(Separated by spaces): "); scanf("%d %d %d",&age,&height,&sc); printf("Please enter a color value(1-Beautiful; 2-General; 3-Crooked melon split jujube (Cuihua)): "); scanf("%d",&yz); if ( ( (age >= 18) && (age <= 30) ) && // Aged between 25 and 30 ( (height >= 165) && (height <= 175) ) && // Height between 165-175 Both must be true ( (sc == 1) ) && // Figure, 1-hot ( (yz == 1) || (yz == 2) ) ) // Appearance, 1-beautiful or 2-average //One of the two is true { printf("Super girl draft qualified, sent to the harem.\n"); } else { printf("If you fail in the super girl draft, you will be given five Liang silver and sent home.\n"); } }

### Assignment operators and assignment expressions

The assignment operator is used to assign the value of a constant, variable or expression to a variable

operator | name | The Works of Liezi | result |
---|---|---|---|

= | assignment | a=3;b=2; | a=3;b=2; |

+= | Plus equals | a=3;b=2;a+=b; | a=5;b=2 |

-= | Minus equals | a=3;b=2;a-=b | a=1;b=2 |

*= | Multiply equal | a=3;b=2;a*=b; | a=6;b=2 |

/= | Division equals | a=3;b=2;a/=b | a=1;b=2 |

%= | Modulo equal (remainder) | a=3;b=2;a%=b; | a=1;b=2 |

a+=b ----------- a=a+b

a-=b ------------a=a-b

#include<stdio.h> int main() { int a=5; int b=(a=5); printf("%d\n",b); int x,y,z; x=y=z=6; printf("Buddy:%d%d%d\n",x,y,z); int d=1; d+=1313;//d=d+1313 printf("%d",d); } //Operation results: 5 Old fellow: 666 1314

### Conditional operators and conditional expressions

When writing programs, we often encounter conditional judgment

For example, if a > b is judged, an operation will be executed when a > b is true, and another operation will be executed when a > b is not true

Expression 1? Expression 2 : Expression 3 #include<stdio.h> int main() { int a = 10; int b = 20; int max = 0; max = (a > b ? a : b);//The effect and meaning of the following sentences /*if (a > b) max = a; else max = b;*/ return 0; } /*Conditional Operator exp1?exp2:exp3 If exp1 is true, it returns exp2; if false, it returns exp3*/

### Bitwise Operators

Bit operator bit is the smallest unit of information in the computer, generally represented by 0 and 1. Bit operator is to operate its operands bit by bit according to its binary form.

### sizeof operator

sizeof operator is provided in C language to obtain the number of bytes of a data or data type in memory

format

sizeof(Type specifier) sizeof expression

#include<stdio.h> typedef struct Worke { char name[10]; int age; long ID; float height; double weight; }worker_t; int main() { printf("char :%lu\n", sizeof( char)); printf("int :%lu\n", sizeof(int)); printf("long :%lu\n", sizeof(long)); printf("float :%lu\n", sizeof(float)); printf("double :%lu\n", sizeof(double)); printf("worker_t:%lu\n", sizeof(worker_t)); printf("1+2 :%lu\n", sizeof(1+ 2)); printf("1+2.5 :%lu\n", sizeof(1+2.5)); printf("\n"); return 0; } char :1 int :4 long :4 float :4 double :8 worker_t:32 1+2 :4 1+2.5 :8

## Operation priority

Operators in C language include monocular operator, binocular operator and ternary operator, and their priorities are as follows:

- Priority 1: various parentheses, such as (), [] and member operators;
- Priority 2: all monocular operators, such as + +, –,!, ~, etc;
- Priority 3 (arithmetic operator): multiplication operator *, division operator /, remainder operator%
- Priority 4 (arithmetic operator): addition operator +, subtraction operator -;
- Priority 5 (shift operator): shift operators <, > >;
- Priority 6 (conditional operator): greater than operator >, greater than or equal to operator > =, less than operator <, less than or equal to operator < =;
- Priority 7 (conditional operator): equal operator = =, not equal operator! =;
- Priority 8 (bitwise operator): bitwise and operator &;
- Priority 9 (bitwise operator): bitwise XOR operator ^;
- Priority 10 (bitwise operator): bitwise OR operator |;
- Priority 11 (logic and operator): logic and operator & &;
- 12th priority logic and operator: logic or operator |;
- Priority 13: three item condition operator?:;
- Priority 14: various assignment operators, such as =, + =, - =, * =, / =;
- Priority 15: comma operation.

Arithmetic operators > shift operators > conditional operators > bitwise operators > logical operators > assignment

//Code in the blog Fanshi coffee to see //Example of operator priority (not involving complex): #include <stdio.h> main() { int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; // ( 30 * 15 ) / 5 printf("Value of (a + b) * c / d is : %d\n", e ); e = ((a + b) * c) / d; // (30 * 15 ) / 5 printf("Value of ((a + b) * c) / d is : %d\n" , e ); e = (a + b) * (c / d); // (30) * (15/5) printf("Value of (a + b) * (c / d) is : %d\n", e ); e = a + (b * c) / d; // 20 + (150/5) printf("Value of a + (b * c) / d is : %d\n" , e ); return 0;} //Operation results: Value of (a + b) * c / d is : 90 Value of ((a + b) * c) / d is : 90 Value of (a + b) * (c / d) is : 90 Value of a + (b * c) / d is : 50

From now on, stick to it and make progress a little bit a day. In the near future, you will thank you for your efforts! |

If something goes wrong again, please correct it.

Newcomers check in, friends, give me a triple (praise, attention, collection)