MCN Professionals | Interview Question of the day
MCN Professionals is starting Industrial Training for MCA-2012 Batch. Have a look at our Industrial Training Program and Course Details.
-----------------------------------------------------------------------------------------------------------------------------------------
Today's Question:
What will be the output of the below code in C language:
int main ()
{
unsigned int cnt;
for (cnt = 5; cnt >= 0; cnt--)
printf("%d ", cnt);
}
Solution:
No, the answer is NOT 5 4 3 2 1 0.
We are printing the value of cnt till its non-negative. cnt is an unsigned int, no matter what, its value is always positive. Hence the loop is Infinite :)
What exactly will be printed is implementation dependent, because when cnt is decremented to hold a negative value (or an unsigned int is assigned a negative value like -1, the result is undefined). But the loop will not terminate gracefully.
Hence, the result is undefined (after 5 4 3 2 1 0 ), but expect a lot of values getting printed before your program crash with a core dump.
---------------------------------------------------------------------------
Interview Questions Archive:
To see all the questions in the category click
here...