Just so you know...

Everything to the right of the assignment operator '=' happens first. So, even 
if you wrote your code as '++a' or as 'a++', 'a' would still get incremented 
before the line gets assigned to 'c'.

--- On Sat, 7/18/09, Dan Presley <[email protected]> wrote:

From: Dan Presley <[email protected]>
Subject: [c-prog] Re: I am not agree with u.
To: [email protected]
Date: Saturday, July 18, 2009, 2:59 AM

OK, never-mind my last posting.  I didn't see the part about init'ing a to 5.  
So I tried your operation:

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    int main(){
    
        int a, c;
    
        a = 5;
    
        c = a/++a;
    
        cout << "a = " << a << "\tc = " << c << endl;

        return 0;
    }

I got a compiler warning about the operation on a (the '++a') being undefined 
when I compiled the source.  When I ran the exe, I got a = 6 and c = 1.  I ran 
the program for both '++a' and 'a++' and got the same result.  My guess is the 
compiler, when executing the increment, is not putting the new value for 'a' in 
a separate space in memory, but incrementing the value in the space already 
reserved for variable 'a'.  Thus the increment is executed prior to the 
division making both sides of the divisor equal values resulting in 1 after 
executing the division operation.  BTW, I use g++, the GCC C++ compiler.  I 
hope this helps solve your dilemma.  Cheers.



------------------------------------

To unsubscribe, send a blank message to 
<mailto:[email protected]>.Yahoo! Groups Links





[Non-text portions of this message have been removed]

Reply via email to