# i++ query

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 11-21-2004
Salem
> b=i++/i++
Ways of evaluating this include...
Code:

```temp = i; i++; b = temp / i; i++;```
Code:

```temp = i; i++; b = i / temp; i++;```
Code:

```temp = i; b = temp / temp; i++; i++;```
Each interpretation is plausable, other interpretations are also plausable, but NONE of them are correct.

http://www.eskimo.com/~scs/C-faq/q3.2.html
Read this again, and play very close attention to what it says about "after" in relation to the postfix operators.

Side effects do NOT follow operator precedence or parentheses, so no amount of
Code:

```b=i++ + i++; b=i++ / i++; b=(i++)/(i++);```
will change the way side effects are applied to the expression.
http://www.eskimo.com/~scs/C-faq/q3.4.html

If you want a guaranteed answer, then write
Code:

`b = i / i ; i += 2;`
The only thing you can GUARANTEE about side effects is that they will happen before the next sequence point (that's the ; in this example).
Sequence points are defined here.
http://www.eskimo.com/~scs/C-faq/q3.8.html
• 11-21-2004
Mister C
It is undefined in C and Salem makes a good point..

Laserlight' comment
Quote:

When the language's standard classifies such an expression as such.
is correct depending on the language: java and C# do have a language standard that classifies these expressions in Java:

Code:

``` public class Test {   public static void main(String args[])   {         int i=10;       i=(i++)/(i++);       System.out.println(i);   } }```
gives the value of 0- it is not undefined.

For your purposes in C, what Salem has said is true.

jc
• 11-21-2004
Prelude
>how can u classify as to wat i am askin as undefined??
Code:

```ISO C Standard Section 6.5, Paragraph 2: Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Further, the prior value shall be read only to determine the value to be stored. 70) ... 70) This paragraph renders undefined statement expressions such as   i = ++i + 1;   a[i++] = i; while allowing   i = i + 1;   a[i] = i;```
If you'd like, I can also give you precise details as to what a sequence point is and is not. Or your could take our word for it that the code is undefined.
• 11-21-2004
quzah
Quote:

Originally Posted by Mister C
is correct depending on the language: java and C# do have a language standard that classifies these expressions in Java:

...snip...

For your purposes in C, what Salem has said is true.

Do you just intentionally bring up unrelated, pointless stuff? No one here cares in the context of this thread if it's defined in Java or C#. We're not talking about either of these languages, so why bring it up to an already confused person?

Quzah.
• 11-21-2004
Mister C
Quote:

Do you just intentionally bring up unrelated, pointless stuff? No one here cares in the context of this thread if it's defined in Java or C#. We're not talking about either of these languages, so why bring it up to an already confused person?

I think it is relative....

What if this member thinks that this behavior is such for other languages that are derived from C? like Java and C#

I have students that ask if this behavior is undefined in other languages other than C.

Maybe I went about this the wrong way- Your right he is confused in C. He does need to understand the how and why it is undefined in C.

I don't think it hurts to point this out.

jc
• 11-21-2004
Prelude
>What if this member thinks that this behavior is such for other languages that are derived from C? like Java and C#
Then he'll avoid using a construct that has questionable merit whether defined or not. A little trivia can be a dangerous thing, and I can easily see your students writing expressions from hell because you told them it was okay.
• 11-21-2004
PING
Quote:

Originally Posted by laserlight
When the language's standard classifies such an expression as such.

okk......so wat u mean to say is that the expression is compiler dependent.....i seem to get it now.......thanx......i know that i was a pain....bt well......sorry bout it

but....i think that u are getting it wrong...i already am satisfied with wat is being said bout i++/i++ being undefined.....for gods sake stop talkin bout it.....

wat my other question was the difference in the way the division is done in normal circumstances and inside the printf() or other functions.....bout the numerator being evaluated first.....please....i am askin bout this and not bout the i++/i++ being defined or not....i understand the thing ......... and i am not confused.......i just want to ask y the compiler is differing in the calculations.....if u can help me on that matter ........ it will be really helpful........
• 11-21-2004
Prelude
>so wat u mean to say is that the expression is compiler dependent
No, not really. If it were implementation-dependent then the implementation would be required to document the behavior. This isn't the case with something that's undefined. The implementation could choose to wipe your hard drive and it wouldn't be required to mention it, or be liable if you lost important files. That's what undefined means: anything can happen, you don't need to be told, and it's your own fault if something breaks.
• 11-21-2004
PING
Quote:

Originally Posted by Prelude
>so wat u mean to say is that the expression is compiler dependent
No, not really. If it were implementation-dependent then the implementation would be required to document the behavior. This isn't the case with something that's undefined. The implementation could choose to wipe your hard drive and it wouldn't be required to mention it, or be liable if you lost important files. That's what undefined means: anything can happen, you don't need to be told, and it's your own fault if something breaks.

okk......so thats wat u mean.....was gettin it in the wrong sense.....but i do think that the expression must be compiler dependent....coz i tried it in ansi c compiler and got the result that i mentioned......tried in borland as well to get the same result......and in the vis c compiler as well.....bt got some diff in the dev c compiler......
but it still doesnt answer the process of execution in the printf() or some other statements like if() and while()

• 11-21-2004
Salem
Do you think you can write without so many dots in your posts?

> but it still doesnt answer the process of execution in the printf() or some other statements like if() and while()
Well that's undefined for you.
Having figured out that
b = i++ / i++;
produces one answer, then no matter what you think that answer is, there is NOTHING to stop the compiler generating a different answer when you do
printf( "Result=%d\n", i++ / i++ );
• 11-21-2004
PING
Quote:

Originally Posted by Salem
Do you think you can write without so many dots in your posts?

> but it still doesnt answer the process of execution in the printf() or some other statements like if() and while()
Well that's undefined for you.
Having figured out that
b = i++ / i++;
produces one answer, then no matter what you think that answer is, there is NOTHING to stop the compiler generating a different answer when you do
printf( "Result=%d\n", i++ / i++ );

sorry bout the dots salem.
i am not askin how the i++ thing works.wat i am askin is bout the way the division is handled in the printf() and if() tiings.i used the i++ thing only as an example to see how the execution of the calculations inside the if() and printf() things work.i am not too sure bout my doubt of the calc goin frm left to right that is the numerator first and then the denominator.
please correct me if i am wrong
• 11-21-2004
Salem
Well post an example without any side effects then (if that issue is done)
• 11-21-2004
PING
Quote:

Originally Posted by Salem
Well post an example without any side effects then (if that issue is done)

i dont think that there is any way of doin so.i am really helpless in putting my point accross.neways.thanx for the help
• 11-21-2004
Dave Evans
Quote:

Originally Posted by PING
sorry bout the dots salem.
i am not askin how the i++ thing works.wat i am askin is bout the way the division is handled in the printf() and if() tiings.i used the i++ thing only as an example to see how the execution of the calculations inside the if() and printf() things work.i am not too sure bout my doubt of the calc goin frm left to right that is the numerator first and then the denominator.
please correct me if i am wrong

OK, here it is: you are wrong.

I'm not sure if it's been spelled out emphatically enough why this is undefined, and how you can tell if an expression leads to undefined behavior in the C language.

Here's my shot at it:

C does not specify the order in which the operands of an operator are evaluated. (The exceptions are &&, ||, ?:, and ",".)

It was previously stated that "order of evaluation" has nothing to do with operator precedence. This is the point that lots of people have trouble absorbing, so I'll repeat it. "Order of evaluation" has nothing to do with operator precedence.

So, given the expression

Code:

`x = (f() + g()) * h();`
It may happen that f() is evaluated first or it may happen that g() is evaluated first, or it may happen that h() is evaluated first. If either f() or g() or h() changes the value of a variable that one (or both of) the others depend(s) on, the behavior is undefined.

If you have any expression that whose behavior or value depends on the order of evaluation of its terms then the behavior is undefined (except for the operators listed above).

Also note that if you have the statement
Code:

`f(a(), b(), c());`

The functions a(), b(), and c() may be evaluated by a given compiler in any order.

A concrete example:

Code:

`printf("%d %d %d", i++, i, ++i);`
If you run this through 10 compilers, or 20, or 30 and they all give the result that indicates that c() is evaluated, then b(), then a(), that still doesn't make the behavior "defined" according to the C standard. If two compilers give different results, it doesn't mean that one is "right" and the other are "wrong". There is no "wrong" compiler behavior here. The result is not required to be any particular thing.

Regards,

Dave
• 11-21-2004
PING
Quote:

Originally Posted by Dave Evans
OK, here it is: you are wrong.

I'm not sure if it's been spelled out emphatically enough why this is undefined, and how you can tell if an expression leads to undefined behavior in the C language.

Here's my shot at it:

C does not specify the order in which the operands of an operator are evaluated. (The exceptions are &&, ||, ?:, and ",".)

It was previously stated that "order of evaluation" has nothing to do with operator precedence. This is the point that lots of people have trouble absorbing, so I'll repeat it. "Order of evaluation" has nothing to do with operator precedence.

So, given the expression

Code:

`x = (f() + g()) * h();`
It may happen that f() is evaluated first or it may happen that g() is evaluated first, or it may happen that h() is evaluated first. If either f() or g() or h() changes the value of a variable that one (or both of) the others depend(s) on, the behavior is undefined.

If you have any expression that whose behavior or value depends on the order of evaluation of its terms then the behavior is undefined (except for the operators listed above).

Also note that if you have the statement
Code:

`f(a(), b(), c());`

The functions a(), b(), and c() may be evaluated by a given compiler in any order.

A concrete example:

Code:

`printf("%d %d %d", i++, i, ++i);`
If you run this through 10 compilers, or 20, or 30 and they all give the result that indicates that c() is evaluated, then b(), then a(), that still doesn't make the behavior "defined" according to the C standard. If two compilers give different results, it doesn't mean that one is "right" and the other are "wrong". There is no "wrong" compiler behavior here. The result is not required to be any particular thing.

Regards,

Dave

okk,so wat that means is that if in a calculation,one function changes the value of a variable which is used by the other function,then the thing is said to be undefined,welll i guess that settles matters.thanx for all yer help guys.will post some questinos that are bugging me ,bt later
ciao
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12