Thread: question about unary operators

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    29

    question about unary operators

    Wondering if anyone can help explain why, in the following code, passing a unary operator on a variable changes the initialized val of variable in main(), but passing arithmetic operator does not?

    Code:
    #include <stdio.h>
    
    
    void displayParam(int a)
    {
    printf("\nInt variable a: %d\n", a);
    }
    
    
    int main(void)
    {
    int a = 0;
    
    
    displayParam(a+1);
    printf("val of a in main (unchanged from initialization) is %d\n",a); //a still = 0:
    displayParam(a++);
    printf("val of a in main (now changed from initialization val) is %d\n",a); //a now = 1
    
    
    return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Because
    Code:
    displayParam(a++);
    is equivalent to
    Code:
    displayParam(a);
    a += 1;
    Devoted my life to programming...

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Because it is a unary operator that has a well-known and very much intended a side-effect.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    Because it is a unary operator that has a well-known and very much intended a side-effect.
    It is not a side-effect. It is the primary intended effect of the operator.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    It is not a side-effect. It is the primary intended effect of the operator.
    Or rather, the primary intended effect of the operator is a side effect
    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

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    LOL. By definition, a side-effect is an effect other than the primary intended effect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    LOL. By definition, a side-effect is an effect other than the primary intended effect.
    Heheh. That said, my funny statement, and I believe iMalc's as well, had this in mind concerning side effects:
    Quote Originally Posted by C99 Clause 5.1.2.3 Paragraph 2
    Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place. (A summary of the sequence points is given in annex C.)
    In other words, iMalc really is correct, and so is my humourous phrasing of the effects of postfix operator++.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading unary operators
    By Niels_M in forum C++ Programming
    Replies: 8
    Last Post: 11-08-2010, 11:48 AM
  2. Overloading unary operators using pointers
    By Niels_M in forum C++ Programming
    Replies: 21
    Last Post: 09-04-2010, 06:15 AM
  3. unary plus
    By cengineer in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2009, 10:48 AM
  4. Unary operators
    By Micko in forum C Programming
    Replies: 2
    Last Post: 11-22-2008, 01:43 PM
  5. Question about operators.
    By Liger86 in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2002, 04:00 PM