2010-09-08

Tricksy

Question:  What does the following pascal code do?
Answer:  Absolutely nothing.

program test;
begin
{var s='test}end.';
 write(s);
}
write('ok');
end.
Why?

The first impression will be that the code will display the text ok because the rest is a pascal comment inside accolades. But the compiler, after an opening accolade, looks for the closing one, and doesn't care where it is. Therefore it doesn't matter that in this case the closing accolade is in the string literal, the comment ends there. The code is then compiled from that point on, the special keyword end is found followed by a dot, and the program ends. The compiler will then warn that there is source code that will never be reached.

The code executed is:

program test;
begin
{var s='test}
end.

Niciun comentariu: