Thread: Explaination of behavior in C

  1. #1
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Explaination of behavior in C

    Given the code
    Code:
    int i = 1;
     int a[4] = {0, 10, 20, 30};
       
     a[i++] = i++;  // a[i] = i; i = i + 1; i = i + 1;
    
     printf(“%d %d %d %d %d\n”,a[0],a[1],a[2],a[3],i);
    The output is in C: 0 1 20 30 3

    However, the output in Java and C# is (with printf statment replace in Java and C#): 0 2 20 30 3

    Just curious as to how C evaluates this vs. Java and C#.
    (Order of evaluation vs. precedence)

    I never really thought about this until now. I really assumed all these languages that evaluate expression in the same way.

    Any thoughts?

    Mr. C.
    Mr. C: Author and Instructor

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I never really thought about this until now
    No, you asked about it 2 years ago and have forgotten
    http://cboard.cprogramming.com/showthread.php?t=24397

    Not to mention this more recent missive
    http://cboard.cprogramming.com/showthread.php?t=54442
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Salem
    > I never really thought about this until now
    No, you asked about it 2 years ago and have forgotten
    http://cboard.cprogramming.com/showthread.php?t=24397

    Not to mention this more recent missive
    http://cboard.cprogramming.com/showthread.php?t=54442
    These threads do not give the answer to the question.

    The question is: How does C evaluate the expression

    Code:
    a[i++] = i++;
    The answer is: The c language standard states that the behavior is undefined, since the order of evaluation is undefined. There are two subexpressions that must be evaluated. Since each has a side effect that affects the value of the other, behavior of the whole expression is undefined.

    Here's a quote from the FAQ at comp.lang.c for an even simpler expression

    http://www.faqs.org/faqs/C-faq/faq//

    3.1: Why doesn't this code:

    a[i] = i++;

    work?

    A: The subexpression i++ causes a side effect -- it modifies i's
    value -- which leads to undefined behavior since i is also
    referenced elsewhere in the same expression, and there's no way
    to determine whether the reference (in a[i] on the left-hand
    side) should be to the old or the new value. (Note that
    although the language in K&R suggests that the behavior of this
    expression is unspecified, the C Standard makes the stronger
    statement that it is undefined -- see question 11.33.)

    References: K&R1 Sec. 2.12; K&R2 Sec. 2.12; ISO Sec. 6.3; H&S
    Sec. 7.12 pp. 227-9.
    It doesn't matter what any particular compiler does with this code, so don't bother telling me what yours does. The behavior is undefined

    Dave

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Dave,

    That is what I was looking for. The specific reference on the evaluation.

    Well the evaluation may be undefined in C but they aren't in in Java and C#-where I spend most of my time now.


    It doesn't matter what any particular compiler does with this code, so don't bother telling me what yours does. The behavior is undefined
    So I hope you were talking about C/c++ compilers-because in the JLS it is not.

    jc
    Last edited by Mister C; 08-14-2004 at 09:03 AM.
    Mr. C: Author and Instructor

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Mister C
    Dave,

    That is what I was looking for. The specific reference on the evaluation.

    Well the evaluation may be undefined in C but they aren't in in Java and C#-where I spend most of my time now.



    So I hope you were talking about C/c++ compilers-because in the JLS it is not.

    jc

    I'm sorry if it was not clear that I meant C and C++. The original post did refer to other languages; I did not comment on them, since this is the C board.

    (I should have said, "don't bother telling me what your C or C++ compiler does. The behavior is undefined.")

    Dave


    Dave

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    These threads do not give the answer to the question.
    Actually the first link does answer the question repeatidly.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Thantos
    Actually the first link does answer the question repeatidly.
    You are correct, of course; I didn't read all of the responses. Sorry

    Dave

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Salem
    No, you asked about it 2 years ago and have forgotten
    http://cboard.cprogramming.com/showthread.php?t=24397
    Heh I actually remember that thread, at that time Mr. C didn't want to accept the fact that such statements are undefined in C.
    Quote Originally Posted by Mister C
    Well the evaluation may be undefined in C
    Great, only took two years!

    Seriously, how does Java handle the evaluation of such statements? What are the rules?
    Last edited by Sang-drax; 08-14-2004 at 01:05 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Mister C
    Well the evaluation may be undefined in C but they aren't in in Java and C#-where I spend most of my time now.
    hmm. i was under the impression that modifying a variable twice in one statement is undefined in Java as well....

    Quote Originally Posted by Sang-drax
    Seriously, how does Java handle the evaluation of such statements? What are the rules?
    i am quite curious as well, enlighten us Mr C

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Mister C's Signature
    Mr. C- C, C++ and Java instructor
    What the heck is C-?

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Thantos
    What the heck is C-?
    thats part of his nick plus a hyphen. kinda like "Thantos- toss master" or something...

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ah I prefer the colon in that situation:
    Thantos: Overlord of Something

  13. #13
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Here is the discussion at JavaRanch on it:

    http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=000775


    Here is the JLS
    http://java.sun.com/docs/books/jls/s....doc.html#4779


    The code I posted above does evaluate in Java.

    Like I said I spend so much time with Java/C# I now forget evaluation in C/C++.

    Well this will be the last semester I teach C-for a while.

    Enrollments are stronger in the other languages

    Lot of good questions at Javaranch.

    Mr. C.
    Mr. C: Author and Instructor

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well this will be the last semester I teach C-for a while.
    There's that C- again. This time I don't get it however.

  15. #15
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I think he meant it as a joke notice the
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explaination of code
    By gtr_s15 in forum C++ Programming
    Replies: 7
    Last Post: 02-02-2006, 02:25 AM
  2. explaination coding for my viva!!! urgent!!!
    By SweeLeen in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2005, 05:35 AM
  3. Need explaination of the following function
    By dianazheng in forum C Programming
    Replies: 5
    Last Post: 06-09-2005, 10:34 AM
  4. Explaination
    By rogerlpe in forum C++ Programming
    Replies: 4
    Last Post: 03-11-2002, 07:28 AM
  5. emotiona post: recap, improvisational explaination of the irrational.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-01-2002, 09:04 AM