Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By grumpy
  • 1 Post By laserlight

question about unary operators

This is a discussion on question about unary operators within the C Programming forums, part of the General Programming Boards category; Wondering if anyone can help explain why, in the following code, passing a unary operator on a variable changes the ...

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

    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
    1,625
    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,305
    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,313
    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%.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    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
    iMalc likes this.
    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

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

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    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++.
    iMalc likes this.
    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

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, 10: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, 09:48 AM
  4. Unary operators
    By Micko in forum C Programming
    Replies: 2
    Last Post: 11-22-2008, 12:43 PM
  5. Question about operators.
    By Liger86 in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2002, 03:00 PM

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