What will be the output of the following C code?
#include
int main() {
int a = 10, b = 20;
if (a = b > 15)
printf("True");
else
printf("False");
return 0;
}
-
A.
False
-
B.
TrueFalse
-
C.
True
-
D.
Compile-time error
Correct Answer:
C. True
Explanation:
In the provided C code, the expression within the if statement is (a = b > 15). Due to the rules of operator precedence, the relational operator (>) is evaluated before the assignment operator (=). Since the value of b is 20, the condition 20 > 15 evaluates to true, resulting in a value of 1. This value is then assigned to variable a. Because the result of the assignment expression is 1, which is considered non-zero and therefore true in C, the program executes the first branch of the if-else statement and prints True.
Click below to open Discussion & Feedback
0 Issues
Please
login to comment or Report Issues.