C-language operators and precedence

Source: Internet
Author: User
Tags arithmetic operators

About the C language operators and priorities, through the collation of many blog data into their own combat, as follows:
A. Arithmetic operations
There are 34 types of operators in C, including common subtraction operations.
1) Addition: + can also indicate a plus sign
2) Subtraction:-can also indicate minus sign
3) Multiplication: * Not in mathematical sense X
4) Division:/Note that the value of 1/2 is 0 and not 0.5
5) residual (modulo operation): the remainder after dividing two integers (note that both sides must be integers, signed only and Lvalue)
Note the point:
1) Int a=10.8;//data precision loss warning. Result for 10--automatic type conversion
2) int a= (int) 10.8;//no warning. Cast 10.8 to integral type--coercion type conversion
3) Double c=10.6+6//result is 16.600000. In a computer, with two numeric values, the values on both sides of the operator need to be of the same type and are automatically type promoted if different. That is, convert 6 to 6.000000 and then participate in the operation.
4) What type of two number is involved in the operation, then what is the result?
int A=10/3 result is 3,
Double B=10/3 result is 3.000000;
Double C=10.0/3 result is 3.333333.
An exact value is required to enforce type conversions.
Double d= (double) 3/2;//converts 3 to a double type to 3.000000, followed by 2 for automatic type promotion to 2.000000 participation, and 1.500000 for the result.
Can also be written as 3/(double) 2. The difference between double e= (double) (3/2) calculates that the value of 3/2 is 1 and then the 1 is forced to type cast, and the result is 1.000000.
B. Assignment operations
1) Simple Assignment
Int a=10+5;a=b=10;//from right to left, cannot be constant on left
2) Compound assignment
a=a+5;=>a+=5;
a=a*5;=>a*=5;
a+=5*6+4;=>a=a+ (5*6+4)
Tip: The budget order depends on two points: the first is the precedence of the operators, and the second is the order of the combinations.

C. Self-increment self-subtraction operation
Int a=10;
There are four ways to add the value of a to 1:
1) a=a+1;
2) A+=1;
3) a++;
4) ++a;
Simple distinction between a++ and ++a.
Int b;
Int a=10;
B=++a; A==11,b==11;a first add 1 to 11, then assign to B
b=a++; A==11,b==10;a first copy the value 10 to b,a and add 1 to 11.
b= (a++) + (++a); A1==10,a2==12,b==22
b= (++a) + (a++); A1==11,a2==11,b==22
Note: int d=10++;//is wrong because it makes no sense to self-add and subtract from constants.

D.sizeof
Function: The number of bytes of memory used to calculate a variable, constant, or a data type.
Basic form: sizeof (variable name | constant | data type), returns a value after completion.
1) sizeof Variable | constant
2) sizeof (variable | constant)
3) sizeof (data type)
Note: Data types must be enclosed in parentheses and cannot be written in the form of the sizeof data type.

E. Relational operators
1) Condition judgment
By default. The correct code for each sentence written in the program is executed, but many times we need to execute a piece of code when a condition is established.
such as logon operations, this can be done using conditional statements.
2) True and False
In C language, the condition is called "true", the condition is not established is called "false".
C language provisions, any value is true and false, any non-0 value is true, only 0 is False, no Boolean type.
3) Relationship Comparison
The result of the relational operation is only two cases, if the condition is true, the value is 1, if the condition is false, the value is 0.
There are 6 types of operators, namely: (1) < (2) <= (3) > (4) >= (5) = = (6)! =
4) Use note
a) = = and! = have equal precedence, the other four relational operators have equal precedence, and the precedence of the former is lower than the latter. If 2==3>1 should calculate 3>1 first
b) in the relational operator, if the precedence is the same, then "left to right" is combined. If 4>3<2, calculate 4>3 first
c) Precedence in relational operators is less than arithmetic operators. such as 3+4>8-2 equivalent (3+4) > (8-2)
D) Exercise 5! =4+2*7>3==10 Calculate 5 First! =18>3==10,5! =1==10,1==10, Fake

F. Logical operators
The result of a logical operation is only two: true (0) and False (1)
1) and && conditions 1&& conditions 2 only the condition 1 and Condition 2 are established, is true otherwise false.
2) or | | Condition 1 | | Condition 2 When one of the conditions 1 or Condition 2 is true, it is false when it is not established.
3) Non-!! Conditional inversion
Note: When a logical operator is operating, the following conditions are ignored, as long as the integrity of the whole can be determined.
Int a=b=10;
Int c= (a>5) && (++b>=11);//At this time a==10,b==11,c==1
Int c= (a<5) && (++b>=11);//At this time a==10,b==10,c==0
Tip: You can use () when you are doing a logical operation, if you don't know the priority of each symbol.

G. Three-mesh operator
Binocular: Requires two values to participate in the operation
Monocular: A numeric participation operation such as! 5
Trinocular: Requires 3 values to participate
Format: Condition? Value 1: Value 2
Judge first? Before the conditions, if the condition is set to return a value of 1, if the condition is not established, return to the condition 2.
The values of A and B are required to be compared to a large number in C c=a>b?a:b
Compares the value of the A,b,c three number and deposits the maximum value in D
Int abmax= (a>b)? a:b;
D=abmax > C? ABMAX:C;

15 Levels of Priority:
1) () []. -
2)! ~-(minus) + +--& (take variable address) * (type) (mandatory type) sizeof
3) */%
4) +-
5) >> <<
6) > >= < <=
7) = = =!
8) &
9) ^
10) |
) &&
12) | |
13)?:
= = = = *=/=%= |= ^= &= >>= <<=
15),
PS: adhesion: 2 13 14 is from right to left, others are from left to right.

C-language operators and precedence

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.