Sunday, January 23, 2011

A C++ Mistake


Consider this piece of code:
int f = 10;

int ff() {
    return f = 5;
}

int main() {
    printf("%d %d\n", ff(), f);
    return 0;
}
As I have expected it to print "5 5" (I was using Dev C++) so did I get, but I didn't know it was wrong until the judge showed me the famous and fabulous "WA". The correct output is "5 10", because, function parameters are though of to be similar as assignment operation and should be performed from right to left. Now it makes sense. But got some penalty for stupid yet adorable Dev C++. However, I've learnt this... that's the good side of it! So, you too be careful if you don't know that already.

4 comments:

  1. what is the GCC version number of your Dev C++? I got "5 10" under GCC 4.4.3.

    BTW, it's one of dark corners of C++.

    ReplyDelete
  2. I used Dev C++ 4.9.9.2 depending on mingw32 package. It has some other problems like although it is possible to use 'long long' but actually it can't handle it. Similarly, it doesn't know anything like 'long double'. Still sometimes I use it... because of it's simplicity in windows regarding that of visual studio 9

    ReplyDelete
  3. maybe code::blocks is a better choice
    http://www.codeblocks.org/

    ReplyDelete