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.


  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++.

  2. I used Dev C++ 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

  3. maybe code::blocks is a better choice