maniakes ([info]maniakes) wrote,
@ 2008-04-28 13:17:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Fun fact of the day
The following statement is true:
c == c++;


(Post a new comment)


[info]daedalus4096
2008-04-28 08:36 pm UTC (link)
All hail the postfix operator!

(Reply to this)(Thread)


[info]triss
2008-04-28 11:09 pm UTC (link)
Hail, hail the lucky ones...

(Reply to this)(Parent)

In most cases....
[info]makellan
2008-04-29 03:58 pm UTC (link)
The statement will return false if c started as -1.

(Reply to this)(Thread)

Re: In most cases....
[info]maniakes
2008-04-29 04:42 pm UTC (link)
Why? Isn't -1 equal to -1?

(Reply to this)(Parent)(Thread)

Re: In most cases....
[info]makellan
2008-04-29 07:57 pm UTC (link)
c = -1;
if(c = c++)
{
printf("this will not be printed");
}
else
{
printf("This will be printed because the 'if' evaluates the result "
"of the assignment to be 0, which is false");
}

(Reply to this)(Parent)(Thread)

Re: In most cases....
[info]daedalus4096
2008-04-29 08:01 pm UTC (link)
That would be true if he were using an assignment operator, which he's not. The double-equals is the equality operator.

(Reply to this)(Parent)(Thread)

Re: In most cases....
[info]daedalus4096
2008-04-29 08:02 pm UTC (link)
Heh, I just realized that I could rephrase my above statement as:

== != =

I'm such a nerd.

(Reply to this)(Parent)

Re: In most cases....
[info]daedalus4096
2008-04-29 08:00 pm UTC (link)
It shouldn't. The whole point of the postfix operator is that the increment takes place *after* the statement is resolved. So, in your case, the statement would resolve to (-1 == -1), which would evaluate as true, and afterwards the value of c would be 0.

(Reply to this)(Parent)(Thread)

Re: In most cases....
[info]makellan
2008-04-29 08:04 pm UTC (link)
Ahh yes, postfix, not prefix. Good point.

(Reply to this)(Parent)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…