what is the difference between a++ and b++ ?
Page 1 of 1 • Share •
what is the difference between a++ and b++ ?
[b]Increase and decrease (++, --)[/b]
Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:
are all equivalent in its functionality: the three of them increase by one the value of c.
In
the early C compilers, the three previous expressions probably produced
different executable code depending on which one was used. Nowadays,
this type of code optimization is generally done automatically by the
compiler, thus the three expressions should produce exactly the same
executable code.
A characteristic of this operator is that it
can be used both as a prefix and as a suffix. That means that it can be
written either before the variable identifier (++a) or after it (a++). Although in simple expressions like a++ or ++a
both have exactly the same meaning, in other expressions in which the
result of the increase or decrease operation is evaluated as a value in
an outer expression they may have an important difference in their
meaning: In the case that the increase operator is used as a prefix
(++a) the value is increased before the result of the
expression is evaluated and therefore the increased value is considered
in the outer expression; in case that it is used as a suffix (a++)
the value stored in a is increased after being evaluated and therefore
the value stored before the increase operation is evaluated in the
outer expression. Notice the difference:
example-1
B=3;
A=++B;
// A contains 4, B contains 4
example-2
B=3;
A=B++;
// A contains 3, B contains 4
In Example 1, B is increased before its value is copied to A. While in Example 2, the value of B is copied to A and then B is increased.
Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:
| c++; c+=1; c=c+1; |
are all equivalent in its functionality: the three of them increase by one the value of c.
In
the early C compilers, the three previous expressions probably produced
different executable code depending on which one was used. Nowadays,
this type of code optimization is generally done automatically by the
compiler, thus the three expressions should produce exactly the same
executable code.
A characteristic of this operator is that it
can be used both as a prefix and as a suffix. That means that it can be
written either before the variable identifier (++a) or after it (a++). Although in simple expressions like a++ or ++a
both have exactly the same meaning, in other expressions in which the
result of the increase or decrease operation is evaluated as a value in
an outer expression they may have an important difference in their
meaning: In the case that the increase operator is used as a prefix
(++a) the value is increased before the result of the
expression is evaluated and therefore the increased value is considered
in the outer expression; in case that it is used as a suffix (a++)
the value stored in a is increased after being evaluated and therefore
the value stored before the increase operation is evaluated in the
outer expression. Notice the difference:
example-1
B=3;
A=++B;
// A contains 4, B contains 4
example-2
B=3;
A=B++;
// A contains 3, B contains 4
In Example 1, B is increased before its value is copied to A. While in Example 2, the value of B is copied to A and then B is increased.

BIT0112-Rokon- Programmer

- Course(s):
- BIT
Blood Group: O+
Posts: 673
Points: 1269

Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Active Topics!

» Understanding Timestamp Based Protocol
» Hi from Newbie
» Compare an image from a list of images in the sql database....tell which image is matching
» Sending email without email ID
» teach yourself c by herbert schildt pdf
» .NET MVC3: Unable to find the requested .Net Framework Data Provider. It may not be installed.
» SAD Slides: 4.1, 4.2
» QA topic Presentation - Urgent Notice