i++ query

This is a discussion on i++ query within the C Programming forums, part of the General Programming Boards category; > b=i++/i++ Ways of evaluating this include... Code: temp = i; i++; b = temp / i; i++; Code: temp ...

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > 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

  2. #17
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    It is undefined in C and Salem makes a good point..

    Laserlight' comment

    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
    Mr. C: Author and Instructor

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #20
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    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
    Mr. C: Author and Instructor

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  7. #22
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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........
    Last edited by PING; 11-21-2004 at 10:24 AM.

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  9. #24
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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()

    please help......

  10. #25
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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. #26
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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

  12. #27
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Well post an example without any side effects then (if that issue is done)

  13. #28
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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

  14. #29
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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
    Last edited by Dave Evans; 11-21-2004 at 12:31 PM.

  15. #30
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple query language
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2008, 04:29 PM
  2. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  3. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  4. dns query
    By bulldog in forum C Programming
    Replies: 6
    Last Post: 02-24-2004, 09:44 AM
  5. WBEM Query too slow
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2001, 05:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21