2010-04-06

C++ riddle 2010-03-19

Notice: You need C++ language knowledge to read this article.

My colleague sometimes gives me a C++ quiz. This is one of such quiz by Peter. What is the output of the following C++ program.


#include <iostream>
#include <string>

int main()
{
std::string prefix("->"), middle(), postfix("<-");
std::cout << "Hi:" << prefix << middle << postfix << std::endl;
return 0;
}


The result is the following.

Hi:->1<-


There is '1' in the middle. Why this happens? Before I give you the
explanation, I will give you some time. Enjoy! To be continued.

No comments: