12/04/2010

The bug in the name of the C++ language.

From the internet, a popular quote goes like this: 

"Of course, while it is true that the ++ operator gives the C++ language its name, it also led to the first joke about the language. C++ haters point out that even the name of the language contains a bug: 'After all, it should really be called ... ++C, because we only want to use a language after it has been improved.' "

We will now see why this joke is so absolutely hilarious. 



int main()
{
int x = 40;
int y = ++x + 1;
std::cout << x << '\n' << y;
}


Output: x = 41, y = 42.

The pre-increment operator first increments the value of (the ++) and then stores it in the variable. Hence, when the code is executed, x becomes 41 AFTER being assigned to .

The post-increment works the other way, the value is first stored in the lvalue before being assigned to the rvalue of the variable being incremented. 


int main()
{
int x = 40;
int y = x++ + 1;
std::cout << x << '\n' << y;
}


Output: x = 41, y = 41

So, technically:

C++ means: First use C and then increment it (improve it). 
++C means: First improve C and then use it. 

So, yes, it should have been ++C!

1 comment:

  1. This must have been the incrementation that is used in a loop.

    ReplyDelete

We Would Love to answer your queries....