Unary operators

This is a discussion on Unary operators within the C Programming forums, part of the General Programming Boards category; Hello to all, I've lost touch with C programming for almost 3 years, so I forgot some subtle details. I ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    Unary operators

    Hello to all,

    I've lost touch with C programming for almost 3 years, so I forgot some subtle details.
    I was solving one problem by writing C program and was surprised to learn that this code:
    Code:
    int main(void)
    {
        int x=3, y=2; 
        
        printf("%d", (x++));
        printf("\n%d", (--y));
        printf("\n%d", (++x));
        printf("\n%d", (y--));
        
        return 0;
    }
    is producing same result as this one:

    Code:
    int main(void)
    {
        int x=3, y=2; 
        
        printf("%d", x++);
        printf("\n%d", --y);
        printf("\n%d", ++x);
        printf("\n%d", y--);
        
        return 0;
    }
    How to explain this?
    It seems that when execution of program comes to %d in printf function, it immediately substitutes this with a value similar like a = b++ no matter there are parenthesis.

    Regards
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,659
    Those parentheses do not matter here since there is only one way to group the single increment/decrement expressions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Well parentheses have no effect on when side effects happen.
    Or if there is an effect, it's entirely arbitrary.

    The only thing you know is that all side effects have happened by the time the next sequence point is reached.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  5. Problem with unary minus overloading
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 01-14-2005, 01:42 PM

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