निम्नलिखित कोड का आउटपुट क्या होगा?
What will be the output of the following code?
int main()
{
int a=5;
while(a=123)
{
printf("RABBIT\n");
}
printf("GREEN");
return 0;
}
Let's analyze the given code step by step:
Code:
int main()
{
int a = 5;
while(a = 123) // This is an assignment, not a comparison
{
printf("RABBIT\n");
}
printf("GREEN");
return 0;
}
Key points:
-
while(a = 123)is an assignment operation, not a comparison. The expressiona = 123assigns the value 123 toa, and the result of the assignment is the value assigned (in this case, 123), which is non-zero. In C, any non-zero value is consideredtruein a boolean context. -
Since 123 is non-zero, the while loop will run indefinitely (infinite loop), printing "RABBIT" over and over again.
-
The
printf("GREEN")will never be reached because the program is stuck in the infinite loop inside thewhile.
Output:
The program will print "RABBIT" an unlimited number of times, and the word "GREEN" will never be printed.
So, the correct answer is:
(A) RABBIT is printed unlimited number of times.
Correct Answer: A) RABBIT is printed unlimited number of times