473,508 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do increment operations work

pai

hi..

I have a simple doubt about increment operators..

Can any one tell me what is really happening..

Eg:
int i=0,ans;

( ans = ++i ++i // This
gives error while compiling )

ans = ++i + +i ;
printf(" %d %d ",ans,i); // it prints 2 1

i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i); // it prints 4 2

i=0;
ans = ++i + ++i + ++i;
printf(" %d %d ",ans,i); // it prints 7 3

How do 4 and 7 came can any one explain...

Thanks
Pai

Jan 27 '06 #1
14 1766
"pai" <gr****@yahoo.com> writes:
I have a simple doubt about increment operators..
I think you mean "question", not "doubt". The words may be synonymous
in your dialect of English, but they mean different things to most of
us.
Can any one tell me what is really happening..

Eg:
int i=0,ans;

( ans = ++i ++i // This
gives error while compiling )

ans = ++i + +i ;
printf(" %d %d ",ans,i); // it prints 2 1

i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i); // it prints 4 2

i=0;
ans = ++i + ++i + ++i;
printf(" %d %d ",ans,i); // it prints 7 3

How do 4 and 7 came can any one explain...


Your indentation is excessive and inconsistent, making it difficult to
read your code.

The C FAQ is at <http://www.c-faq.com/>. Read question 3.2. Then
read the rest of section 3. Then read the rest of the FAQ.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 27 '06 #2
pai
hi..

Sorry for not having a clear english dialect.
I am not able to understand how is the increment works in program 1
and in program 2 why there is a compile error.

here is the code..

---------------------------------------
Program 1
-------------------------------------
main(){

int i=0,ans;

ans = ++i + +i ;
printf(" %d %d \n",ans,i);

i=0;
ans = ++i + ++i ;
printf(" %d %d \n",ans,i);

i=0;
ans = ++i + ++i + ++i;
printf(" %d %d \n",ans,i);

}

The result
2 1
4 2
7 3

----------------------------------------
program 2
------------------------------------------------
Another program is

main(){
int i=0,ans;

ans = ++i ++i ;
printf(" %d %d \n");

}

This gives compile error
test1.c:4: In function `main':
test1.c:4: error: invalid lvalue in increment
test1.c:9: error: syntax error before "i"

Thanks
Pai

Jan 27 '06 #3
pai wrote:

hi..

I have a simple doubt about increment operators..

Can any one tell me what is really happening..

Eg:
Your code is incomplete, and horribly difficult to read with that much
indentation (one reason may be using TABs in the original file, which
is almost always a bad idea).

You're missing:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int i=0,ans;
These are better put on separate lines, or at least horizontally spaced.
Choose from:

int i = 0, ans;

and

int i = 0;
int ans;

( ans = ++i ++i //
This
gives error while compiling )
This is not a legal C comment (nor are C++ style ones, at least for on
compiler).

Of course this is a syntax error. Sequence <expression1> <whitespace>
<expression2> is not legal in C. You have `++i` as both expression 1
and 2. If you expected something different (and I can't see what that
might have been), keep in mind that compiler follows `maximum munch`
rule, i.e. it reads the code sequentially and tries to make sense of
the longest possible legal sequence of characters. In your case these
are both `++i`s, but stringing them together does not make sense.

ans = ++i + +i ;
printf(" %d %d ",ans,i); // it prints 2 1
This is undefined behaviour. The `++i` bit can be executed at any time
during the evaluation of the expression (but before the sequence point
marked with `;` in your case, and after the previous one, which in your
case is the previous `;`). The same holds true for taking the value of
`i` for the second part of your expression (`+` in `+i` is unary plus,
which does exactly the same as +2 does in mathematics, i.e. nothing
much). Your compiler has chosen to first do `++i` then take value of
`i`, and evaluate expression as 2. It might have decided to do it the
other way around, and give you 0.

i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i); // it prints 4 2
U.B. here as well, and your compiler chooses to be consistent and first
do both `++i`s, then perform addition, and give you 4 as a result.

i=0;
ans = ++i + ++i + ++i;
Same again here, but now the compiler apparently decides to deal with
the first two `++i`s first, add the results, only then perform the
third `++i`, and add that to the running total.

NB, all these are just my double-guessing what your compiler decided to
do on this particular occasion. Next week it may decide to do something
entirely different. Do not do these things (and do not rely on what
this compiler does when switching to a different one).
printf(" %d %d ",ans,i); // it prints 7 3
It is actually lucky that you get anything out of this program, as you
did not terminate the printf() with '\n' to force flushing of stdout.

You've missed these as well:

return 0;
}

How do 4 and 7 came can any one explain...


As Keith pointed out elsethread reading the C FAQ at
http://www.c-faq.com/ (maybe even the whole thing, not just 3.2 he
suggested) would help you know the answers to such questions (or even
not to try to ask them of the compiler at all) immediately.

Cheers

Vladimir

--
Did you know ...

That no-one ever reads these things?

Jan 27 '06 #4

"pai" <gr****@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

hi..

I have a simple doubt about increment operators..

Can any one tell me what is really happening..

Eg:
int i=0,ans;

( ans = ++i ++i // This
gives error while compiling )

ans = ++i + +i ;
printf(" %d %d ",ans,i); // it prints 2 1

i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i); // it prints 4 2

i=0;
ans = ++i + ++i + ++i;
printf(" %d %d ",ans,i); // it prints 7 3

How do 4 and 7 came can any one explain...

Thanks
Pai


Read the FAQs! This is one of the most common questions.
Basically, you can't auto-increment a variable more than once between
control points. So any statement that increments a variable more than once
leads to undefined behavior.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Jan 27 '06 #5

pai wrote:
hi..

I have a simple doubt about increment operators..

Can any one tell me what is really happening..

Eg:
int i=0,ans;

( ans = ++i ++i // This
gives error while compiling )

As well it should.
ans = ++i + +i ;
printf(" %d %d ",ans,i); // it prints 2 1

i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i); // it prints 4 2

i=0;
ans = ++i + ++i + ++i;
printf(" %d %d ",ans,i); // it prints 7 3

How do 4 and 7 came can any one explain...

Thanks
Pai


Any expression of the form i = i++, i = j++ + j++, a[i] = i++, and
f(i++, i++) will invoke undefined behavior, meaning the compiler is
under no obligation to yield a correct or meaningful result, so trying
to figure out exactly *how* those results came about is often a futile
exercise.

The reason these expressions are undefined is because you are
attempting to read and modify an object more than once between
successive sequence points. Because of the way the autoincrement and
autodecrement operators work, there's no guarantee that the side effect
will be applied immediately after the expression is evaluated; it's
possible for the compiler to defer applying side effects until after
some or all of the expressions have been evaluated. In the last case,
it *looks* like the compiler is doing this:

1. evaluate ++i -> 1
2. add 1 to i (1)
3. evaluate ++i -> 2
4. add 1 to i (2)
5. evaluate 2 + 2 -> 4
6. evaluate ++i -> 3
7. add 1 to i (3)
8. evaluate 4 + 3 -> 7
9. assign 7 to ans

Or it could be doing something else completely. Such is the nature of
undefined behavior.

Jan 27 '06 #6
"pai" <gr****@yahoo.com> writes:
Sorry for not having a clear english dialect.
Context: This was about the "doubt" vs. "question" thing. We've seen
that enough around here that we're (sort of) getting used to it, but
using "question" will make it easier to understand.
I am not able to understand how is the increment works in program 1
and in program 2 why there is a compile error.

here is the code..

---------------------------------------
Program 1
------------------------------------- [snip] ans = ++i + ++i ; [snip] ans = ++i + ++i + ++i;
I already answered that in the article to which you're replying.
Once again:

] The C FAQ is at <http://www.c-faq.com/>. Read question 3.2.
] Then read the rest of section 3. Then read the rest of the FAQ.
----------------------------------------
program 2
------------------------------------------------
Another program is [snip] ans = ++i ++i ; [snip] test1.c:9: error: syntax error before "i"


Yes, that's a syntax error. The subexpression "++i" appears twice,
with no operator in between. What were you expecting it to do?

And please read <http://cfaj.freeshell.org/google/> and follow its
advice.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 27 '06 #7
ans = ++i ++i
This gives error while compiling because you did not terminate each of
the ++i statement
with a ';' character.
It should be like this:
ans = ++i;ans = ++i;
OR:
ans=(++i) + (++i);

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

here, ans = 1 + 1=2.
because you first increment the value of i by 1 and copy this value in
ans and then u add the current value of i in ans again.
thus, ans=2 and i=1 and so it prints 2 and 1.

i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i);

here u initialise i=0, thus no value is there in i.
now you add the twice increment i and add this value in ans.Since ans
had already value of 2 (in the last operation), now its value is 4
since i=2 ans ans=2+2=4;

i=0;
ans = ++i + ++i + ++i;
printf(" %d %d ",ans,i);

Here again you initialise i=0.Now ans is having value 4 already and i
is 3 times incremented and then added to ans. and so ans had the value
4+3=7

and so that is why you get the values 4 and 7 of ans

Jan 29 '06 #8
Lemor said:
i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i);

here u initialise i=0, thus no value is there in i.
Wrong. The value is 0. This /is/ a value. It is not "no value".
now you add the twice increment i and add this value in ans.Since ans
had already value of 2 (in the last operation), now its value is 4
since i=2 ans ans=2+2=4;


No, the behaviour is undefined because:

"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the value
to be stored." - C89, 3.3.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 29 '06 #9
Fred Kleinschmidt wrote:
[...]
ans = ++i + +i ; [...] ans = ++i + ++i ; [...] ans = ++i + ++i + ++i;
[...] Read the FAQs! This is one of the most common questions.
Basically, you can't auto-increment a variable more than once between
control points. So any statement that increments a variable more than once
leads to undefined behavior.


Actually, you only need to modify it once, as long as you access it more
than once. The following are just as UB as the above, despite the single
modification to "i":

i = i++;
x = i + ++i;

Note that the first example by the OP only modifies "i" once.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jan 30 '06 #10
On Fri, 27 Jan 2006 18:36:19 +0000 (UTC), "Vladimir S. Oka"
<no****@btopenworld.com> wrote:
pai wrote:
( ans = ++i ++i //
This
gives error while compiling )


This is not a legal C comment (nor are C++ style ones, at least for on
compiler).

Of course this is a syntax error. Sequence <expression1> <whitespace>
<expression2> is not legal in C. You have `++i` as both expression 1


It is if expression2 begins with a prefix operator that is also a
binary (infix) operator, namely + - & -- although semantically the
meaning changes from identity to addition, negation to subtraction, or
address to bit-and. Syntactically * would also work but semantically
the (right) operand of prefix-* must be a pointer and infix-* not.
and 2. If you expected something different (and I can't see what that
might have been), keep in mind that compiler follows `maximum munch`
rule, i.e. it reads the code sequentially and tries to make sense of
the longest possible legal sequence of characters. In your case these
are both `++i`s, but stringing them together does not make sense.
The maximal-munch rule applies at the lexical level only. ++i ++i
lexes as ++ i ++ i rather than ++ i + + i or + + i + + i. There is no
single token ++i or ++i++i.

<snip good points about semantics being Undefined>
As Keith pointed out elsethread reading the C FAQ at
http://www.c-faq.com/ (maybe even the whole thing, not just 3.2 he
suggested) would help you know the answers to such questions (or even
not to try to ask them of the compiler at all) immediately.

Actually Keith did suggest the whole FAQ as well as (after) 3.2.

- David.Thompson1 at worldnet.att.net
Feb 6 '06 #11
On Sun, 29 Jan 2006 19:49:10 -0500, Kenneth Brody
<ke******@spamcop.net> wrote:
Fred Kleinschmidt wrote:
Read the FAQs! This is one of the most common questions.
Basically, you can't auto-increment a variable more than once between
control points. So any statement that increments a variable more than once
leads to undefined behavior.


Actually, you only need to modify it once, as long as you access it more


Actually it's enough to modify once and read once if that read is not
(used) "to [compute] the value to be stored" e.g. x = (i=3) + i;
than once. The following are just as UB as the above, despite the single
modification to "i":

i = i++;
I believe 'modify' in Standardese is interpreted as any store, even of
the existing value, so that one is two stores.
x = i + ++i;

Note that the first example by the OP only modifies "i" once.


- David.Thompson1 at worldnet.att.net
Feb 6 '06 #12
On 2006-02-06, Dave Thompson <da*************@worldnet.att.net> wrote:
On Sun, 29 Jan 2006 19:49:10 -0500, Kenneth Brody
<ke******@spamcop.net> wrote:
Fred Kleinschmidt wrote:

> Read the FAQs! This is one of the most common questions.
> Basically, you can't auto-increment a variable more than once between
> control points. So any statement that increments a variable more than once
> leads to undefined behavior.


Actually, you only need to modify it once, as long as you access it more


Actually it's enough to modify once and read once if that read is not
(used) "to [compute] the value to be stored" e.g. x = (i=3) + i;
than once. The following are just as UB as the above, despite the single
modification to "i":

i = i++;


I believe 'modify' in Standardese is interpreted as any store, even of
the existing value, so that one is two stores.


Are you allowed to do two stores of the same value?

i=++i; /* probably not */

but...

f(i=0,i=0) /* is f guaranteed to receive two 0's? is i guaranteed to be
0 afterwards? */

or even
f(i=1,i=3) /* is f guaranteed to receive 1,2? is i guaranteed to be odd?
*/
Feb 6 '06 #13
[regarding undefined behavior in expressions that modify objects
multiple times, and so on]
On 2006-02-06, Dave Thompson <da*************@worldnet.att.net> wrote:
I believe 'modify' in Standardese is interpreted as any store, even of
the existing value ...

In article <sl**********************@random.yi.org>
Jordan Abel <ra*******@gmail.com> wrote:Are you allowed to do two stores of the same value?
No, not technically at least.
i=++i; /* probably not */
This one is definitely out, and could even conceivably misbehave
in some sysetms. (The misbehavior would, I think, depend on the
compiler "knowing" that multiple modifications are not allowed and
therefore assuming that "++i" has not modified "i" because "i=" is
going to modify "i".)
but...

f(i=0,i=0) /* is f guaranteed to receive two 0's? is i guaranteed to be
0 afterwards? */
This is technically illegal, although I find it hard to imagine
any way to cause it to break.
or even
f(i=1,i=3) /* is f guaranteed to receive 1,2? is i guaranteed to be odd?
*/


(Presumably the comment should read "1,3".) This one is also
technically illegal, and one can imagine hardware on which it
behaves badly (multiple simultaneous stores in a VLIW machine, in
which stores are implemented by circuits that are not "wire-OR"
capable, resulting in overheating of the circuits and burning out
parts of the computer[%]).

[% One might object: "But no one would ever build such a machine."
Well, actually, someone *did*. I once programmed a piece of DEC
hardware in which the manual advised taking caution to avoid
programming the bit-slice units to write to the same data lines at
the same time, as they were not wire-OR capable and this could thus
burn out the drive transistors. Curiously, the DEC-supplied driver
-- which was buggy, which is why I was programming the device --
did just what the manual advised against. Perhaps this was why
the device was persistently flaky, although the symptoms did not
match up with the way the hardware must have worked.]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 6 '06 #14
On 6 Feb 2006 05:57:58 GMT, Chris Torek <no****@torek.net> wrote:
[regarding undefined behavior in expressions that modify objects
multiple times, and so on]
(attribution to Jordan Abel <ra*******@gmail.com> lost)

<snip>
but...

f(i=0,i=0) /* is f guaranteed to receive two 0's? is i guaranteed to be
0 afterwards? */


This is technically illegal, although I find it hard to imagine
any way to cause it to break.

Purely as a thought experiment:

On x86 and S/360 and maybe more "sub rega from rega" or "xor" is
(often?) cheaper than "load constant/literal 0 to rega".

I can just barely imagine multiple unsynchronized arith units doing
rega@3 := rega@0 - rega@2
rega@5 := rega@1 - rega@4 # result original value not cleared

- David.Thompson1 at worldnet.att.net
Feb 13 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

98
14307
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
31
2332
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
45
4048
by: Robbie Hatley | last post by:
Hello, group. I've been doing too much C++ programming lately, and I'm starting to become rusty at some aspects of the C way of doing things, esp. efficient low-level data copies. ...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.