C language learning notes (3) -- operators and expressions (I)

Source: Internet
Author: User
Tags arithmetic operators bitwise operators

 

1 operator and Expression

 

1.1 operator classification:

 

Arithmetic Operator: used for arithmetic operations, including +,-, *, And.

 

Relational operators: used for comparison operations, including>, <, =, and ,! =.

 

Logical operators: used for logical operations, including &, |, and ,! Three.

 

Bitwise operators: bitwise operations by binary, including &, | ,~ And <.

 

Value assignment operator: used for value assignment operations, including =, + =, and & =.

 

Conditional OPERATOR: A three-object operator, used to evaluate conditions (? :).

 

Comma OPERATOR: Used to combine several expressions into one (,).

 

Pointer Operator: Used to get content * and to get address &.

 

Number of nodes OPERATOR: used to calculate the number of bytes occupied by the Data Type sizeof.

 

Special operators: (), [], members (->,.), etc.

 

1.2 Operation Sequence

 

A. Function compute is preferred.

 

B. The second is forced type conversion.

 

C. Auto-increment, auto-subtraction, and inverse.

 

D. multiplication, division, and addition and subtraction.

 

E. Enclose the brackets first.

 

Note: When two consecutive operators appear in an expression, it is best to use spaces to separate them. For example, a ++ B must be written as a ++ B or a ++ B.

 

2. Data Type Conversion

 

2.1 forced type conversion: converts the displayed expression to the required type.

 

Method 1: (type name) (expression ).

 

Method 2: (type name) expression.

 

For example, force type conversion.

 

 

# Include "stdio. h"

# Define PI 3.1415926

Main (){

Int m, n;

Float x, y;

X = 2.5;

Y = 4.8;

M = (int) (x + y);/* convert the floating point type to an integer type */

N = (int) x + m/3;/* forcibly convert x to an integer */

M = n % 4;/* integer remainder */

X = x + y;

Printf ("m = % d, n = % d, x = % f, f = % f", m, n, x, y );

}

Note: Forced type conversion does not change the type attributes of the original variables and expressions, but is only used to convert to the required type.

 

2.2 implicit type conversion: Different numeric types of integer, single-precision, and double-precision types can be mixed for calculation, and implicit type conversion is involved in the operation process.

 

Implicit type conversion rules are: from low to high conversion. For example, char and short can be converted to int.

 

3 Arithmetic Operators and arithmetic expressions

 

3.1 basic arithmetic operators: Include the plus (+), minus (-), minus (*), minus (/), and minus (%) operators.

 

3.2 Special Arithmetic Operators: auto-increment and auto-subtraction.

 

Prefix format: The operation object is first Auto-incremented or auto-subtracted, and then the value of the operation object is referenced as the value of the expression.

 

Suffix format: first reference the value of the computing object as the value of the expression, and then auto-increment or auto-subtraction of the computing object.

 

Example: + + a is equivalent to a = a + 1, -- a is equivalent to a = A-1;

 

A ++ is equivalent to a = a + 1, a -- is equivalent to a = A-1;

 

Note: it can be seen that there is no difference in use alone, but the expressions have different effects.

 

Sample Code:

 

 

# Include "stdio. h"

# Define PI 3.1415926

Main (){

Int I = 6, j = 6, k = 6, h = 6, m, n, x, y;

M = I ++;/* suffix form self-added */

N = ++ j;/* prefix self-increment */

X = k --;/* suffix form auto-subtraction */

Y = -- h;/* suffix form auto-subtraction */

Printf ("\ n I = % d, m = % d, j = % d, n = % d", I, m, j, n );

Printf ("\ n k = % d, x = % d, h = % d, y = % d", k, x, h, y );

}

4. Value assignment operators and value assignment expressions

 

4.1 simple value assignment operator

 

A simple value assignment is used to store the value of an expression to a variable, for example, a = 4;

 

Note: The left side of the value assignment operation must be a variable;

 

The priority of the value assignment operation is relatively low ,;

 

4.2 compound assignment operator

 

Is to combine the operation and value assignment, such as + =,-=, and so on.

 

For example, m + = 3 is equivalent to m = m + 3;

 

4.3 assignment expression operation

 

Note the following when using expressions:

 

4.3.1 composite value assignment operator.

 

Sample Code:

 

 

# Include "stdio. h"

# Define PI 3.1415926

Main (){

Int x = 2, y, z;

X * = 3 + 2;/* is equivalent to x = x * (3 + 2 )*/

Printf ("(1) x = % d \ n", x );

X * = y = z = 4;/* is equivalent to y = z = 4, x = x * y */

Printf ("(2) x = % d \ n", x );

X = y = 1;

Z = x +-1;/* equivalent to y = X-1, x = X-1 */

Printf ("(3) x = % d *** z = % d \ n", x, z );

Z = x ++ * ++ y;/* is equivalent to y = y + 1, z = x * y, x = x + 1 */

Printf ("(4) x = % d *** y = % d *** z = % d \ n", x, y, z );

}

4.3.2 value assignment conversion: If the types on both sides of the operator are different but both are numerical values or characters, C will automatically convert the corresponding types.

 

Sample Code:

 

 

# Include "stdio. h"

# Define PI 3.1415926

Main (){

Int a, B = 322;

Float x, y = 8.88;

Char c1 = 'k', c2;

A = y;

X = B;

A = c1;

C2 = B;

Printf ("% d, % f, % d, % c", a, x, a, c2 );

}

 

From letthinking's column

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.