Thread: Unary operators

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

    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:
    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:

    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.

    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
    Those parentheses do not matter here since there is only one way to group the single increment/decrement expressions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

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, 02:14 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07: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, 02:42 PM