C-language operator excerpt

Source: Internet
Author: User
Tags arithmetic operators bitwise true true

Priority level

Operator

Name or meaning

Use form

Combination direction

Description

1

[]

Array subscript

array name [constant expression]

Left to right

()

Parentheses

(expression)/function name (formal parameter list) =

.

Member selection (object)

Object. Member name

-

Member selection (pointer)

Object pointers, member names

2

-

Minus sign operator

-expression

Right to Left

Monocular operator

Type

Forcing type conversions

(data type) expression

++

Self-increment operator

+ + variable name/variable name + +

Monocular operator

--

Self-decrement operator

--Variable name/variable name--

Monocular operator

*

Value operator

* Pointer variable

Monocular operator

&

Fetch address operator

& Variable Name

Monocular operator

!

Logical non-operator

! An expression

Monocular operator

~

Bitwise inverse operator

~ Expression

Monocular operator

sizeof

Length operator

sizeof (expression)

3

/

Except

An expression/expression

Left to right

Binocular operator

*

By

Expression-expression *

Binocular operator

%

Remainder (modulo)

Integer expression/integer expression

Binocular operator

4

+

Add

An expression + an expression

Left to right

Binocular operator

-

Reducing

Expression-expression

Binocular operator

5

<<

Move left

Variables << expressions

Left to right

Binocular operator

>>

Move right

Variables >> expressions

Binocular operator

6

>

Greater than

Expressions > expressions

Left to right

Binocular operator

>=

Greater than or equal

Expression->= expression

Binocular operator

<

Less than

Expressions < expressions

Binocular operator

<=

Less than or equal

Expression-<= expression

Binocular operator

7

==

Equals

Expression-= = Expression

Left to right

Binocular operator

!=

Not equal to

Expression! = Expression

Binocular operator

8

&

Bitwise-AND

Expressions & Expressions

Left to right

Binocular operator

9

^

Bitwise XOR OR

An expression ^ expression

Left to right

Binocular operator

10

|

Bitwise OR

Expression-expression

Left to right

Binocular operator

11

&&

Logic and

Expressions && Expressions

Left to right

Binocular operator

12

||

Logical OR

An expression | | An expression

Left to right

Binocular operator

13

?:

Conditional operators

Expression 1? Expression 2: Expression 3

Right to Left

Trinocular operator

14

=

Assignment operators

variable = expression

Right to Left

/=

Assign value after addition

Variable-/= expression

*=

Multiply post-Assign value

Variable-*= expression

%=

Assign value after modulo

Variable-%= expression

+=

Add after Assignment

Variable + = expression

-=

Reduced-value Assignment

Variable-= expression

<<=

Assign value after left shift

Variable-<<= expression

>>=

Move right

Post-Assignment value

Variable->>= expression

&=

Bitwise AND post-assigned values

Variable-&= expression

^=

Bitwise XOR or post-assignment

Variable-^= expression

|=

Bitwise OR post-assigned value

Variable-|= expression

15

,

Comma operator

expression, expression,...

Left to right

Left-to-right sequential operation

Description

Operators of the same precedence, the order of operations is determined by the binding direction.

Because of the number of operators in the C language, the complexity of precedence, difficult to remember, for the above operators, we can be summed up into several formulas to facilitate memory:

Priority formula

parenthesis member first; parentheses operator [] () member operator. -

All monocular second; All monocular operators such as + + + (positive)-(negative) pointer operation *&

Multiplication Yozo, plus minus four; this "remainder" means the residual operation is%

Shift five, relationship six, shift operator:<< >>, relationship:> < >= <=, etc.

Equals (and) unequal row seventh; = = = = =

Bits and Xor and bits or; these are bitwise operations: Bits and (&) XOR (^) bits or (|)

"Three points of the world" eighty or ninety;

logical or heel; logical Operator: | | and &&

12 and 11; Note order: Priority (| |) bottom-to-priority (&&)

The condition is higher than the assignment, the three-mesh operator precedence is ranked to 13-bit only than the assignment operator and the "," high

Lowest comma operation level! The comma operator has the lowest precedence

Coercion type conversion operator (data type)//++--Self-increment decrement operator//arithmetic operator: */% +-//relational operator: > >= < <= = =!=//logical operator:  && | |!//conditional operator (Trinocular operator)?:/ /assignment Operator: =/= *=%= + =-=//comma operator:,//Expression: an equation consisting of an operator, a constant, a variable, or an expression that has a deterministic data type with a value of # include <stdio.h>int main (int argc, const char * argv[]) {    printf ("%.2f\n", 3.14*5*5);    printf ("%d\n", 56*67);    int a=10;    int b=;    printf ("a+b =%d\n", a+b);        return 0;}

The self-increment decrement operator//++--int main (int argc,const char *argv[]) {    int a=10;    int b=20;    printf ("A=%d b=%d\n", a++,b--);//a++ <==> a=a+1    //After the statement execution is complete, change the value of a B to    printf ("a =%d B =%d\n", A, b);    Before the statement is executed, change the value of a, B,        printf ("A=%d b=%d\n", ++a,--b);        printf ("A=%d b=%d\n", (++a + a++),--b); The value of//a is undefined, and the value of the variable cannot be modified more than once in a C-language statement        //printf ("b =%d\n",--b +--b);    return 0;}

Arithmetic operators: */% +-int main (int argc,const char *argv[]) {    int a=10, b=20;    printf ("a*b =%d\n", a*b);    printf ("A/b =%d\n", A/b);    printf ("a%%b =%d\n", a%b);//%d%f%ld  percent output one% of the first% is used to escape        printf ("a+b =%d\n", a+b);    printf ("A-B =%d\n", A-a);    printf ("value =%d\n", a%b*a);    return 0;}

Relational operators: > >= < <= = =!=int Main (int argc,const char *argv[]) {    int a = ten, b=;    printf ("val =%d\n", a>b);    printf ("val =%d\n", a<b);        printf ("(a>=b) =%d\n", a>=b);    printf ("(a<=b) =%d\n", a<=b);    printf ("(a==b) =%d\n", a==b);    printf ("(a!=b) =%d\n", a!=b);    return 0;}

Logical operators:  && | |!//&& (logical AND logical multiplication)//expression 1   expression 2 ...  Expression n Entire expression//true true//False         true/                   /True         false//False false//                   Conclusion: For logical AND, an expression in an expression is false, Then the value of the entire expression is False//| | (Logical OR logical plus)//expression 1 expression 2 entire expression//true True//False True//      true fake//False false       //conclusion: For logic Plus, as long as an expression is true, the value of the entire expression is true//! (logical non)//0 is False  !0 is true//expression     entire expression//true         false//False         true int main (int argc, const char *argv[]) {    int a= 0;    int b=;    printf ("a&&b =%d\n", a&&b);    printf ("a| | b =%d\n ", a| | b);    printf ("!a =%d\n",!a);        return 0;}

Conditional operator (trinocular operator)?:/ /expression? Expression 1: Expression 2//If the value of the expression is true, the value of the entire expression is the value of expression 1 otherwise the value of expression 2//macro defines a function # # min (A, B) ((A<B)? ( A):(B)) #define MAX (A, A, (a) (A>b) ( A):(B)) #define MIN2 (A,b,c) (((A<b) ( A):(B)) <c)? ((a<b)? (a):(B)): c) int main (int argc, const char *argv[]) {    int a=5;    int b=20;    int c=10;    printf ("val =%d\n", a<b?a:b);    printf ("min =%d\n", Min (A, b));    printf ("max =%d\n", Max (A, b));    printf ("min2 =%d\n", Min2 (A, B, c));    return 0;}

Assignment operator: =/= *=%= + =-=int Main (int argc,const char *argv[]) {    int a=20;//    int b=10;    A = ten;    printf ("A=%d\n", a);    printf ("(a/=b) =%d\n", a/=b);//<==> a = A/b    //a=2    printf ("(a*=b) =%d\n", a*=b);//<==> a= a*b    // A=20    printf ("(a%%=b) =%d\n", a%=b);//<==> a= a%b;    a=0;    printf ("(a+=b) =%d\n", a+=b);//<==> a= a+b;    a=;    printf ("(a-=b) =%d\n", a-=b); <==> a= A-B;        return 0;}

Comma operator:,//Expression 1, expression 2, expression 3, expression 4,....... The expression n//the entire expression value is the value of the expression n int main (int argc,const char *argv[]) {    int a=20;    int b=10;    int c=30;    int d=40;    printf ("val =%d\n", (c=a+b,d=a-b,a*b));    return 0;}

Operator precedence//parentheses > Monocular operators > Arithmetic operators (*/% greater than +-) > Relational operators (> >= < <= greater than = = =) > && >| | > Conditional operator > Assignment operator > Comma operator//parentheses increase the priority of an expression int main (int argc,const char *argv[]) {    int a=10;    float f=3.14;    printf ("val =%f\n", a+f);//Implicit conversion    printf ("val =%d\n", A + (int) f);//Truncate    printf ("val =%d\n", (int) (A+F));//Display conversion ( Forced type conversion)        char ch = -34;    int num = 2045678;    printf ("value =%d \ n", ch + num),//char    -to-int//bit-extension for positive, high 0, 1    return 0 for negative highs;} Conclusion: 1. Floating-point type data is followed by integer data, and the integer data is promoted to floating-point type data//2. For integer data char short to int long long long  type//3. Only char short type data operations, char short to int type//4.float to double  long dou ble type on par//5. Signed type data is aligned to unsigned type data

C-language operator excerpt

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.