C language learning notes (iv) -- operators and expressions (II)

Source: Internet
Author: User

 

1. Comma operator and comma expression

 

1.1 comma expression format: expression 1, expression 2, expression 3,..., expression n;

 

This expression is used to calculate expression 1, expression 2, expression n, and the value of the last expression.

 

1.2 priority of a comma expression: lowest.

 

1.3 values of the comma expression:

 

Sample Code:

 

 

# Include "stdio. h"

Main (){

Int a, B, c;

A = 1, B = 2, c = a + B + 3;

Printf ("% d, % d, % d \ n", a, B, c );

C = (a ++, a ++ = B, a-B);/* variable c saves the value of the comma expression */

Printf ("% d, % d, % d \ n", a, B, c);/* the comma expression is used to evaluate the Order */

}

The output result is as follows:

 

1, 2, 6

 

4, 2, 2

 

2. Relational operators and expressions

 

True and false values in 2.1C: C language does not contain Logical Data. Therefore, the C language specifies that the true value is expressed by an integer constant 1. All non-0 expressions and the logical values of constants are 1, the integer constant 0 indicates the dummy value. Only constants whose expression value is 0 and the logical value of the expression are 0.

 

Link expression example code:

 

 

# Include "stdio. h"

Main (){

Char x = 'M', y = 'n ';

Int n;

N = x <y;/* obtain the value of the relational expression x <y */

Printf ("% d \ n", n );

N = x = Y-1;/* Get the relational expression x = the value of the Y-1 */

Printf ("% d \ n", n );

N = ('y '! = 'Y') + (5 <3) + (Y-x = 1);/* Get the value of the relational expression x = Y-1 */

Printf ("% d \ n", n );

}

 

Program running result:

 

1

 

1

 

2

 

Relational expressions participate in arithmetic operations:

 

 

# Include "stdio. h"

Main (){

Int a, B, c;

A = B = c = 10;/* variable a, B, c assigned initial values */

A = B = c;

Printf ("a = % d, B = % d, c = % d \ n", a, B, c);/* pay attention to the difference between equal signs and value assignment numbers */

A = (B + (c ++> 2 ));

Printf ("a = % d, B = % d, c = % d \ n", a, B, c );

/* The relational expression value can be set in the c ++> 2 operation. values 1 and B are added to a for storage */

A = B> c * 100;

Printf ("a = % d, B = % d, c = % d \ n", a, B, c );

/* Pay attention to the priority of calculation when using relational expressions */

}

Program running result:

 

A = 1, B = 10, c = 10

 

A = 11, B = 10, c = 11

 

A = 0, B = 10, c = 11

 

3 logical operators and logical expressions

 

3.1 basic logical operators include: single object operators !, Binary operators &, |.

 

Example code of logic and operator:

 

 

# Include "stdio. h"

# Define PI 3.1415926

Main (){

Int m, n;

Int k;

K = (m = 0) & (m = 1 );

Printf ("% d, % d \ n", m, k );

K = (m = 1) & (m = 0 );

Printf ("% d, % d \ n", m, k );

K = (m = 2) & (m = 1) & (m = 0);/* test multi-layer logic and Operation Rules */

Printf ("% d, % d \ n", m, k );

M = 0, k = 0;

N = k ++ & m ++;

Printf ("% d, % d, % d \ n", k, m, n );

M = 0, k = 0;

N = m ++ & k ++;/* test expression k ++ & m ++ and m ++ & k ++ */

Printf ("% d, % d, % d \ n", k, m, n );

}

Program running result:

 

0, 0

 

0, 0

 

0, 0

 

1, 0, 0

 

0, 1, 0

 

Example code of logic or operator:

 

 

# Include "stdio. h"

Main (){

Int m, n;

Int k;

K = (m = 0) | (m = 1);/* Calculation rule of logical or operation */

Printf ("% d, % d \ n", m, k );

K = (m = 1) | (m = 0 );

Printf ("% d, % d \ n", m, k );

K = (m = 2) | (m = 1) | (m = 0);/* multi-layer logic or operation rule test */

Printf ("% d, % d \ n", m, k );

M = 0, k = 0;

N = ++ m | ++ k;

Printf ("% d, % d, % d \ n", k, m, n );

M = 0, k = 0;

N = ++ k | ++ m;

Printf ("% d, % d, % d \ n", k, m, n ); /* test expression ++ k | ++ m and ++ m | ++ k ++ */

}

Program running result:

 

1, 1

 

1, 1

 

2, 1

 

0, 1, 1

 

1, 0, 1

 

Example code of logical and logical or hybrid operations:

 

 

# Include "stdio. h"

Main (){

Int m, k;

K = (m = 0) | (m = 1) & (m = 2);/* expression interpreted as (m = 0) | (m = 1) & (m = 2 ))*/

Printf ("% d, % d \ n", m, k );

K = (m = 2) | (m = 1) & (m = 0);/* expression interpreted as (m = 2) | (m = 1) & (m = 0 ))*/

Printf ("% d, % d \ n", m, k );

K = (m = 2) & (m = 1) | (m = 0);/* The expression is interpreted as (m = 2) & (m = 1) | (m = 0 )*/

Printf ("% d, % d \ n", m, k );

K = (m = 0) & (m = 1) | (m = 2);/* The expression is interpreted as (m = 0) & (m = 1) | (m = 2 )*/

Printf ("% d, % d \ n", m, k );

}

Program running result:

 

2, 1

 

2, 1

 

1, 1

 

2, 1

 

4. Conditional operators and conditional expressions

 

4.1 condition operator "? : "Is the only three-object operator in C language. It has a higher priority than the value assignment operation.

 

Expression syntax: expression 1? Expression 2: expression 3

 

The execution order is: first solve expression 1. If it is 0, execute expression 2; otherwise, execute expression 3.

 

Sample Code:

 

 

# Include "stdio. h"

Main (){

Char ch;

Scanf ("% c", & ch);/* enter a character and assign it to the variable ch */

Ch = (ch> = 'A' & ch <= 'Z ')? (Ch + 'a'-'A'): ch;/* If the character is an upper-case letter, convert it to a lower-case letter */

Printf ("\ n % c", ch );

}

5sizeof Operator

 

5.1sizeof is a single-object operator in C language. The calculation object is a variable name, expression, or data type identifier. The function provides the storage size of the operands in bytes.

 

5.2 syntax format: sizeof (expression) or sizeof expression or sizeof (data type)

 

6. Obtain the address Operator

 

6.1 The calculation object of the address operator can only be a variable, and the calculation result is the storage address of the variable.

 

7 mixed operation

 

Sample Code:

 

 

# Include "stdio. h"

Main (){

Int x, y = 1, z;

X = (9 + 6) % 5 >=9% 5 + 6% 5 )? 1:0;/* (9 + 6) % 5> = 9% 5 + 6% 5) is equivalent to (9 + 6) % 5)> = (9% 5 + 6% 5) the value is 0 */

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

Y + = z = x + 3;/* y + = z = x + 3 Equivalent z = x + 3, y = y + z */

Printf ("y = % d \ t z = % d \ n", y, z );

X = y = z = 1;

-- X & ++ y | z ++;/* Mixed logic and logic or operation */

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

}

Program running result:

 

X = 0

 

Y = 4 z = 3

 

X = 0 y = 1 z = 2


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.