The difference between i++ in Java and i++ in C

Source: Internet
Author: User

In Java:

Using the intermediate variable mechanism:
i = i++;

Equivalent:

temp = i;
i = i + 1;
i = temp; (i.e. i = i++ = temp)


int a = 0,sum;

In Java, after using the expression a++, a immediately adds one;


For sum = a++ + a++;

Java: After the first a++ executes, the value of a is immediately added to 1, and the value of a is 1 before the second a++ executes.

A = 0;
A = a++ + a++;

Temp1 = A; --Temp1 = = 0;
A = a+1; A = a+1;
Temp2 = A; --Temp2 = = 1;
A = a + 1; A = a+1;
A = Temp1 + Temp2; A = Temp1 + Temp2;

================================================

int a = 0, sum;

A = a++;
System.out.println (a);

The result is: 0
-------------------------------------

int a = 0, sum;

sum = a++ + a++;

SYSTEM.OUT.PRINTLN (sum);
System.out.println (a);

The result is: 1 and 2

--------------------------------------
int a = 0, sum;

A = a++ + a++;

System.out.println (a);

The result is: 1

--------------------------------------
int a,sum;
A = 0;

sum = a++ + ++a;

SYSTEM.OUT.PRINTLN (sum);
System.out.println (a);

The result is: 2 and 2


=============================================================================================================== =

In the C language:

+ + is a unary operator: + and = Both are two-tuple operators

For unary operators, the return value is: i++ is the value of I, ++i is the value of I plus one

The i++ operation means that a self-increment instruction is generated after the instruction of the two-dollar operator is generated (that is, after the "+" and "=" operations have been completed, the increment is added).

The ++i operation means that a self-increment instruction is generated before the instruction generated by the two-dollar operator (that is, it is self-increasing before the "+" and "=" operations are executed).

=============================================================================================================== =


int i = 0, sum;

Test unary operators

If printf ("%d", I); The result is: 0

If printf ("%d", i++); The result is: 0

If printf ("%d", ++i); The result is: 1

----------------------------------------------------------------------
int i = 0, sum;

Testing the two-dollar operator

sum = i++;

printf ("%d", sum);
printf ("%d", I);

The result is: 0 and 1

If you change to:

i = i++;
printf ("%d", I);

The result is: 1

----------------------------------------------------------------------

int a = 0, sum;


sum = a++ + a++;

printf ("%d\n", sum);
printf ("%d\n", a);

The result is: 0 and 2

Analysis:

First a++ is executed (at this point, the value of a is 0),

Then execute the second a++ (at which point A has a value of 0),

Finally, the addition and assignment operations are performed (at this point a value of 0,sum is 0 + 0 = 0),

After the addition operation is finished, perform a two plus action (at which point A is 2)

If you change to:

A = a++ + a++;

printf ("%d", a);

The result is: 2

----------------------------------------------------------------------

int a = 0,sum;

A = a++ + ++a;
printf ("%d\n", a);

Process:
First, a self-increment of 1, which is brought by ++a (even if a++ on the far left). At this point a = 1

We then calculate the two operand of the + sign.
Because they are actually a (this involves the compiler implementation problem, most C compilers will combine the two operands, that is, the variable a),
The value is 1, so that the return value of a++ + ++a is 1+1=2, and then the value of a++ + ++a is assigned to a, when a = 2

Finally, complete the self-increment instruction brought by a++, at which point A is the value of 2+1=3, which is a = 3

The result is: 3


If you change to:
sum = a++ + ++a;
printf ("%d", sum);
printf ("%d", a);


Process:
First, a self-increment of 1, which is brought by ++a (even if a++ on the far left). At this point a = 1

Then, we calculate the + number of the two operands.
Because they are actually a (this involves the compiler implementation problem, most C compilers will combine the two operands, that is, the variable a),
The value is 1, so that the return value of a++ + ++a is 1+1=2, and then the value of a++ + ++a is assigned to sum, when sum = 2,a = 1

Finally, complete the self-increment instruction brought by a++, at which point A is the value of 1+1=2, which is a = 2

Results are: 2 and 2

=============================================================================================================== =

A = a++ ++a; Differences in Java and C

In Java, a++ is executed first,
C, first execute the ++a







The difference between i++ in Java and i++ in C

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.