Thread: Side effect of various operations

  1. #1
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630

    Side effect of various operations

    I know you guys dont like answering h.w. questions but im trying to study for my test and cant find the answers to this in my notes or any book. My teacher wants us to know what some common expressions that are used in C++ more for the side-effect than the returned value. I dont understand what he means, like how i++ returns a temporary value or something? Also whats the side effect of evaluating X = 4, and whats the value of the expression? Thanks for the help
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    i++

    ++ returns a 1 and adds it to the original int.

    x=4

    the value of x is 4 and any value that was initially in x is overwritten.
    Blue

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Well I know that whatever is in x is overwritten, but I dont think that is what he is asking. I remember reading somewhere a long time ago that = returns a lvalue or something but i cant remember where i read that too many damn books!
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  4. #4
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    He could be refering to this:

    i++ is a post expression operator (not sure if that's the right term) anyways the increment is done after the use of the variable.

    so
    Code:
    int num = 5;
    
    cout << num++ << endl;
    outputs 5 and leaves num with a value of 6.
    A better example.
    Code:
    int num = 5;
    int sum = 0;
    
    sum = num + num++;
    sum is 10 when the expression is evaluated and num is 6 if we use it later.

    ++i on the other hand is done before the expression is evaluated, so it's the opposite.

    Now the x=4 thing.
    When comparing variables in boolean expressions it is a common and hard to find error if you use = instead of ==. The problem lies in the fact that
    if( x = 4 )
    is allowable syntax but what it does is assign 4 to x and then evaluate x. In C/C++ 0 (or negative number) is false and a positive number is true, which could result in a problem like this:
    Code:
     
    if( x = 4 ) {
     cout << "This statement will always run\n";
    }
    else {
     cout << "This statement is never run\n";
    }
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL example cube is black, white, grey only
    By edwardtisdale in forum Windows Programming
    Replies: 7
    Last Post: 09-22-2007, 02:37 PM
  2. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 04:30 AM
  3. Strange side effects?
    By _Elixia_ in forum C Programming
    Replies: 4
    Last Post: 08-16-2005, 03:25 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM