当前位置:网站首页>C language operators and expressions

C language operators and expressions

2022-06-24 22:41:00 Tengban monster

Catalog

Preface

Assignment operator 、 Assignment expression

1. Assignment operator

2. Assignment expression

3. Assignment statement

4. usage

Cast cast character

1. Automatic conversion

2. Coercive transformation

Arithmetic operator 、 Arithmetic expressions

1. Arithmetic operator

2. Arithmetic expressions

Auto increment and auto decrement operator

sizeof Operator

Compound assignment operator


Preface

Operators are used to process data . Using operators to connect variables and constants conforms to C Expressions of linguistic grammar rules are called expressions . Single constant 、 A variable or function is a simple expression .

It is divided according to the number of operands carried by the operator ,C There are three categories of operators in languages :

Monocular operator : An operator with only one operand . Such as :++ Operator .

Binocular operator : Operator with two operands . Such as :+、-、*、/、% Operator .

Ternary operator : Operator with three operands . Such as :? Operator .

Assignment operator 、 Assignment expression

1. Assignment operator

Symbol : =

function : Assign the value of the expression on the right to the variable on the left . The left and right sides here are not interchangeable .

Binocular operator : There are operands on both sides .

for example :

int a,b,c;
a=1314;//a=1314
b=a;//b=1314
c=a+b;//c=2628

2. Assignment expression

Let's look at an example :

a=520

Be careful : Expressions have values , The value of the assignment expression is the value of the assigned variable . The above example a=520 The value of this assignment expression is a Value 520.

3. Assignment statement

C Language policy , Any expression with a semicolon at the end becomes a statement .

So assignment expression plus semicolon is assignment statement .

Look at the example again :

a=365;

4. usage

Continuous assignment : Right binding , That is, to operate from right to left .

Look at examples :

a=b=c=d=666;

Cast cast character

1. Automatic conversion

When = When the data types on both sides are different ,C Language will make = The data type of the expression value on the right becomes = The data type of the variable on the left .

Look at examples :

int a=3.65;//a=3

2. Coercive transformation

Change if you want to :

float a=1.79,b=2.1;

that (int)a The value is 1, Yes , It's rounding down ,(int)(a+b) Then it is a+b The result turns into int type , Its value is 3.

Arithmetic operator 、 Arithmetic expressions

1. Arithmetic operator

Symbol : +  -  *  /  %

function : I believe you can add (+)、 reduce (-)、 ride (*)、 except (/) You can understand , Remainder (%) What is it? ?

According to my habit , Let's take a look at the examples first :

int a=10%3;//a=1
int b=9%3;//b=0

It is often used in the follow-up study , There is another thing called mold taking , These two mean the same , Is the remainder of the division of two numbers . The remainder is very easy to use , It's a skill job .

Be careful :C Language policy , The quotient of the division of two integers is still an integer , Rounding down .

for example :5/2 The result is 2, If you want to get 2.5, Should be written as 5.0/2 or 5/2.0.

Be careful :% It can only be used for integer operations .

2. Arithmetic expressions

An expression whose operators are all arithmetic operators is called an arithmetic expression . I believe everyone can understand .

Auto increment and auto decrement operator

Symbol : ++  - -

seeing the name of a thing one thinks of its function , Look at the example , Feel for yourself :

int i=j=k=l=6;
i++;//i=7
j--;//j=5
++k;//k=7
--l;//l=5

Okay , I believe you should have doubts in your mind . Here's an explanation :

Prepositive operations :++i、--i: Increase or decrease before operation .

Post operation :i++、i--: Calculate first and then increase or decrease .

Look at examples :

int i=j=k=l=6,look;
look=i++;//look=6,i=7
look=j--;//look=6,j=5
look=++k;//look=7,k=7
look=--l;//look=5,l=5

Be careful :++ and -- Operators can only be used with variables , Cannot be used for constants and expressions .

            Two + And two - There is no space between .

            Auto increment and auto decrement are often used in loop statements .

            You cannot use... On a variable consecutively , Such as ++i++ It's illegal. .

sizeof Operator

Symbol :sizeof

function : Get the memory occupied by variables and data types ( Number of bytes ).

Be careful :sizeof in size and of There is no space between .

Compound assignment operator

Symbol :+=、-=、*=、/=、%=、&=、|=、^=、<<=、>>=

function : See the following example :

a+=1 Equivalent to a=a+1

a*=2 Equivalent to a=a*2

原网站

版权声明
本文为[Tengban monster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211225199136.html