Rules for comma expressions and self-increment decrement operators and type conversions

Source: Internet
Author: User

Comma operator

The comma operator has the lowest precedence;

The comma operator is used to split multiple expressions;

The result of a comma-expression consisting of a comma operator is that the result of the last expression

Attention:

int i;

i = 3,4,5,;

The value of I obtained from the above statement is 3

i = (3, 4, 5);

The resulting value of I is 5

Summarize:

++i Pre-increment: First add 1 to the value of I and then use the value of 1 to participate in all subsequent operations


i++ lag self-increment: First use the original value of the variable to participate in all subsequent operations, until all operations of this statement are completed, the value of the variable is added minus 1


Lag self-increment in the table of special performance: First use the original value to participate in the condition of judgment, and then immediately from increase or decrease;


The special behavior of the lag self-increment in the comma expression:


int i = 3, j = 4, K;

K = (++i, j + +, i++ * ++j);

printf ("%d%d%d\n", I, J, K);

Results:

i = 5;

j = 6

K = 24

Summarize:

Hysteresis increment occurs immediately when a comma operator is encountered, except for a parameter comma in a function

Data type conversions

1.1) Automatic type conversion

In one operation, if the data types of the two operands participating in the operation are inconsistent, the C language will convert the data type to its own custom and follow the following guidelines:

The short-integer is closer to the long integer type;

Simple data types move closer to complex data types;

Here are two examples:

5.0 + 1/2

=5.0 +0

=5.0+0.0=5.0

5+1/2.0=5+1.0/2.0=5+0.5=5.0+0.5=5.5

Mathematical formula: 1/1+1/2+1/3+.....+1/n

int i;

Double s = 0;

for (i = 1; I <= n; i++)

s + = 1.0/i;

1.2. Forcing type conversions

Grammar:

(data type conversion) expression

s + = (double) 1/i;//forced type conversion is also an operator, which is the lowest of the monocular operator

Coercion of type conversion with human will as principle

s + = (double) (1/i);//Just get 0.0

Forcing a type conversion operation is absolutely impossible to change the data type of a variable

1.3 Automatic coercion of type conversions

This operation only occurs in assignment operations.

char C = 97.14159;

Some precision will be lost at the time of assignment, the output lowercase a

Double f = ' A ';

Output 65.000

Conversion principle: If the data types on both sides of the assignment operator are inconsistent, the value of the right expression of the assignment operator is automatically converted by the C language, and is based on the left value data type on the left side of the assignment operator;


Rules for comma expressions and self-increment decrement operators and type conversions

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.