🚀 Hurry! Offer Ends In
00 Days
00 Hours
00 Mins
00 Secs
Enroll Now
X

निम्नलिखित कोड का आउटपुट क्या होगा?

What will be the output of the following code?

int main()
{
int a=5;
while(a=123)
{
    printf("RABBIT\n");
}
printf("GREEN");
return 0;
}

A
RABBIT is printed unlimited number of times
B
RABBIT GREEN
C
Compiler error
D
GREEN
Explanation

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:

  1. while(a = 123) is an assignment operation, not a comparison. The expression a = 123 assigns the value 123 to a, 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 considered true in a boolean context.

  2. Since 123 is non-zero, the while loop will run indefinitely (infinite loop), printing "RABBIT" over and over again.

  3. The printf("GREEN") will never be reached because the program is stuck in the infinite loop inside the while.

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

Latest Updates