Consider the following code in C:
void main()
{
int a=0;
for(printf("\nA"); a; printf("\nB"));
printf("\nC");
printf("\nD");
}
When I compile it using Turb C++ version 3.0 and gcc-4.3.4, I get the following as the output in BOTH the cases :
A
C
D
However, if I compile the following code:
void main()
{
for(printf("\nA"); 0; printf("\nB"));
printf("\nC");
printf("\nD");
}
The output by gcc-4.3.4 is the same as in the previous case but turbo c++ 3.0 produces the following output :
A
B
C
D
First of all, I have no idea what's happening here! Plus, how come the output by the gcc compiler is the same for both the codes but in the case of turboc++ 3.0 compiler, the output is different? Can someone please shed some light?
EDIT :
Actually someone was asked this question in an interview for an IT company and when he failed to give the answer, the interviewer gave this explanation. But I find this to be stupid. How can you ask someone to use a "bug" as if it's a "facility" provided by the language? For it to be called a "facility" and "technique", whether we pass 0 as a literal in the second expression or a variable whose value is 0, the outcome should have been the same.
Am I wrong concluding that the interviewer was very dumb to ask a question like that and that it shows his incompetence?