pre-/postfix in-/decrement

This is a discussion on pre-/postfix in-/decrement within the C Programming forums, part of the General Programming Boards category; And again I am not so sure if what a tutor at my university is telling us about C is ...

  1. #1
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Question pre-/postfix in-/decrement

    And again I am not so sure if what a tutor at my university is telling us about C is actually true.

    Today, he gave us an example of a valid (?) C expression:

    i+++++i---i

    Then he used some brackets to make this expression easier to read:

    (i++)+(++i)-(--i)

    So, I did write a small program to explore how my compiler (Borland C++ Command Line Tools 5.5.1) would handle this:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int a, i = 5;
    	
        a = i+++++i---i;
    	
        printf("%d", a);
    	
        return 0;
    }
    These are the error messages thrown at me by the compiler:
    Error E2277 test18.c 8: Lvalue required in function main
    Warning W8004 test18.c 12: 'i' is assigned a value that is never used in function main


    I've already looked into the ANSI C Standard and found out that the order in which the post-/prefix operations are executed is undefined. It is only defined that they must be carried out between the ';' of the previous expression and the ';' at the end of the current expression.

    Now, my question: Why does the compiler give the error messages above? And why does the program "work" if I put the expression in brackets (as shown above) - although the output of the program is the value I've initialized int i with and the compiler still gives the warning message(as shown above) but no more error message.

    Thank you for your help.

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    your prof is a moron. i'm a a bit shaky on this, but i'm pretty sure the expression

    i+++++i---i

    MUST be parsed as

    i ++ ++ + i -- - i

    if there are no grouping parentheses or explicit whitespace.

    second a single variable may not be modified more than once between sequence points, which no matter how you parse it, that expression will do.
    Last edited by moi; 11-13-2002 at 02:49 PM.
    hello, internet!

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    oh btw, gcc 2.8.1 throws "illegal lvalue in increment" at that slop of garbage. ask your idiot if he's ever tried any of his stupid expressions in a *gasp* compiler
    hello, internet!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    .. another why doesn't my (undefined) code work question...

    Read this and this.

    There's no point in discussing the output from undefined behaviour code...

    [edit]Doh! beat.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Originally posted by moi

    MUST be parsed as

    i ++ ++ + i -- - i

    if there are no grouping parentheses or explicit whitespace.
    I can edit my post 2... boi
    Last edited by OneStiffRod; 11-13-2002 at 03:41 PM.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  6. #6
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by moi
    your prof is a moron
    He's not a Prof. but "just" a Doc.
    But I totally agree with you.

    Thank you very much for your help, moi.

    And also thank you, Hammer, for the FAQ-Links.

    Next week I will try to enlighten this "moron".
    The funny thing is, I am the only non-computer-science student attending this course ... nobody else was alarmed by this undefined expression.

    @OneStiffRod:
    ??Why don't you proof your statement??

    At least, moi has the ANSI C Standard on his side. I've just found this:
    EXAMPLE 2 The program fragment x+++++y is parsed as x ++ ++ + y, which violates a constraint on
    increment operators, even though the parse x ++ + ++ y might yield a correct expression.
    If I interpret this correctly, moi is correct.
    Last edited by Sargnagel; 11-13-2002 at 03:17 PM.

  7. #7
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by OneStiffRod
    I can edit my post 2... boi
    i edited my post 12 minutes before you posted yours; your quote of my post even contains the edited version, and not the original!

    on top of that, the only thing i changed was the addition of "or explicit whitespace", which does not change the meaning of the post and is just a minor clarification (because the sample expression does NOT contain any whitespace anyway).

    this is not the first time you have very harshly and grossly inaccurately criticized my posting. you don't need to be a c master to contribute here, but try to be a bit more levelheaded, or at least, barring that, right.

    i'm not taking your editing out of your wrong accusation from your post as a concession because if it had been, you would have replaced it with something more along the lines of "oops, my bad" and not "i can edit my post too" (which leaves you no ground to stand on anyway as i have outlined above).
    hello, internet!

  8. #8
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Oh BS, I don't believe that you only changed that part, I had your post marked at 9:48 and the last - negagtives were messed.

    Why else would you change it, I responded 10 mins later and added the quote - it's then that I realized you corrected yourself.

    Just make sure you are correct, becuase I'll be there to jump down your throat to correct you, just returning the FAVOR.

    Late.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  9. #9
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by OneStiffRod
    Oh BS, I don't believe that you only changed that part, I had your post marked at 9:48 and the last - negagtives were messed.

    Why else would you change it, I responded 10 mins later and added the quote - it's then that I realized you corrected yourself.

    Just make sure you are correct, becuase I'll be there to jump down your throat to correct you, just returning the FAVOR.

    Late.
    not my problem that you think and then write 10 minutes later without thinking again in that interval. as to what i changed in my edit, lie all you want, and see if i care one bit.
    hello, internet!

  10. #10
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669

    Wink

    There's nothing to care about, just be correct OK>>>.

    I'm just returning the FAVOR you did for me, OK BUDDY....
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

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

    Cool

    Hey I got slammed on something similar to this over a month ago!!!

    By the way I am NOT the instructor who Sargnagel is taking this class from
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. I need to play a pre recorded vox file using C
    By m.sudhakar in forum C Programming
    Replies: 4
    Last Post: 11-17-2006, 05:59 PM
  3. New compiler - Weird errors -,-.
    By Blackroot in forum C++ Programming
    Replies: 8
    Last Post: 08-27-2006, 07:23 AM
  4. Pre Processor output
    By siavoshkc in forum C++ Programming
    Replies: 5
    Last Post: 08-11-2006, 05:32 PM
  5. Conflicting types of typedef error
    By advocation in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2005, 05:26 PM

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