当前位置:网站首页>C language operator priority formula

C language operator priority formula

2022-06-23 06:54:00 zetor_ major

Vinegar jar sour filling  
The taste falls and the blessing beans jump  

common 44 Operators  

vinegar - Elementary ,4 individual : ( ) [ ] -> Point to structural members . Structural members  
The altar - Monocular ,9 individual : ! ~ ++ -- - Minus sign ( type )   * The pointer & Address fetch sizeof length ( Combination direction : From right to left )
acid - The arithmetic ,5 individual : * / % + - reduce  
taste - Displacement ,2 individual : << >> 
irrigation - Relationship ,6 individual : < <= > >= == be equal to  != It's not equal to  
taste - Bit logic ,3 individual : & Bitwise AND  ^ Bitwise XOR | Press bit or  
fall - Logic ,2 individual : && Logic and || Logic or  
jump - Conditions ,1 individual , Three orders : ? :                                                               ( Combination direction : From right to left )
blessing - assignment ,11 individual : = += -= *= /= %= >>= <<= &= ^= |=                       ( Combination direction : From right to left )
bean - comma ,1 individual : , 
There are only three types of combinations from right to left : assignment 、 Monocular and trinocular , The others are from left to right .


Be careful : I wrote the program today The priority of relational operation symbols and bit logic is reversed . Try to use parentheses , There's no problem .
Such as :if(P1OUT&BIT0==0)P1OUT ^=BIT0;         // Judge p1.0 Whether the output is 0, Yes, just flip , On LED.
This line of program is wrong . You can put parentheses around , namely if((P1OUT&BIT0)==0), Or directly :P1OUT|=BIT0;

The operator with the highest priority is not really an operator , They are a special kind of operation .() It's about functions ,[] Array related , and -> And . It's structural members .
The second is the unary operator , All unary operators have the same priority , So in my opinion In real operators, they have the highest priority , And because they are Combined from right to left , therefore *p++ And *(p++) There is no doubt about equivalence .
Next comes the arithmetic operator ,*、/、% Of course, the priority is higher than +、- High .
The shift operator follows .
In the next relational operator ,< <= > >= than == != A higher level , It's not easy to understand .
All logical operators have different priorities ( Monocular operators go out ,! and ~)
Of logical bitwise operators " And " Than " or " high , and " Exclusive or " Between them .
Followed by && Than || high .
Next is the conditional operator , Assignment operator and comma operator .

stay C In language , Only 4 An operator specifies the direction of the operation , They are &&、| |、 Condition operator and assignment operator .
&&、| | We all calculate the value of the expression on the left first , When the value of the left expression can determine the value of the whole expression , The value of the expression on the right is no longer evaluated . Such as a = 0 && b; && The left bit of the operator 0, Then the expression on the right b No longer judge .
In the conditional operator . Such as a?b:c; First judge a Value , According to a The value of is right b or c One of them is evaluated .
The assignment expression specifies that the expression on the right is evaluated first , So make a = b = c = 6; Make it possible .

 

原网站

版权声明
本文为[zetor_ major]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230537386398.html