C language operators notes

Arithmetic Operators

+    -     *    /    %

Note: For 1 / operator said that if the two operands are integers still get integer (get is "business"), in order to get a float, then both operands must have a float.

           2. For operator% for both operands are integers necessary (to obtain the "I" number).

 

Shift operators

>> right shift operator

<< the left shift operator

 

Right shift operator is divided into:

1. The logical shift              to the right to give up, left fill 0

2. arithmetic right shift             to the right to give up on the left make up the original sign bit

The left shift operator with a logical shift right;

 

Bitwise operator

&                  // bitwise (bit corresponding to binary 0, the result is 0)

|                  // bitwise or (corresponding to binary 1 bit, the result is 1)

^                 // bitwise exclusive OR (compared with the corresponding bits of the same binary 0, 1 was different)

Note: two operand bits operator must be an integer

Specific, can be achieved by two integers exchange operator ^

a = a^b;

b = a^b;

a = a^b;

Assignment operator

 =                   Note the difference between = and == difference

*=                 Example:a*=2      a = a*2

/=                                

+=

-=

%=                                a%=2     a = a%2

>>=

<<=

Unary operators

++ - + (n)   - (minus)    ~ * & (address)    ! sizeof 

Note that unary operators its priority

A difference between the front and rear ++ a ++ a: a front ++ 1 represents a first increase, and then use a ++ represents a rear first use, plus 1;

Similarly - too

sizeof operator is not a function that can be used to find the size of a number of bytes, can also be used to find a size of type

int a = 0;

sizeof(a);

sizeof( int );

sizeof can be used to find the number of array elements

Relational Operators

>=

<=

>

<

! =       To test two numbers "not equal"

==      tests that the two number "equal"

Be careful not to mistakenly written == when writing code =, the resulting error

Logical Operators

&&                expression 1 && expression 2 expression 1 and expression 2 are true, the result was true       Note: If the expression 1 is false, the expression 2 will not be calculated

||                   expression 1 || 2 expression expression 1 or expression 2 is established, the results are true           Note: If the expression 1 is true, the expression 2 will not be calculated

Attention on specific examples, please refer to https://blog.csdn.net/weixin_44915811/article/details/90417381

Conditions operation marks

1 expression ? Expression 2 : Expression 3 (ternary operator)  

Usage rule: a calculation expression, if true, the result of the expression to 2 as a whole a result, the expression 2 is false, the result of the expression to 3 as a result of a whole

Example:        a>b ? a : b

Comma expression

Expression 1, expression 2, expression 3, ...... n-expression;

Comma expression is a good plurality of expressions separated by commas, and the calculation rule is: calculated from Expression 1 to Expression n, to n as the result of the expression of the entire expression.

The subscript operator reference

 [ ]

Operands: expression array name or an integer value +

int arr[10] = {0};

arr, 10 of [] of the two operands      Note: [] within the variable is not only integer constant expression or plastic

Function call

( )

Operand is a function call operator: the function name and argument passing

char arr[] = "hello world";

int ret = MyStrlen (arr); for the function call operator, the function name and parameters are his operand; missing function name becomes (arr) is obviously incorrect, missing parameters MyStrlen () is not correct    function itself except parameters do not need to .

Access a structure member

Structures . Member Name 

Structure pointer -> member name

struct Stu

{

     char name[20];

     char sex[5];

};

struct Stu s;

struct Stu* ps = &s;

struct Stu s.name = "John Doe";

ps -> sex = " 男";

 

 

 

 

 

 

Published 20 original articles · won praise 9 · views 909

Guess you like

Origin blog.csdn.net/weixin_44915811/article/details/99844078