What will be the output of the following C code?
#include <stdio.h>
int main()
{
int x = 2, y = 0;
int z = (y++) ? y == 1 && x : 0;
printf("%d\n", z);
return 0;
}
#include <stdio.h>
int main()
{
int x = 2, y = 0;
int z = (y++) ? y == 1 && x : 0;
printf("%d\n", z);
return 0;
}A. 0
B. 1
C. Undefined behaviour
D. Compile time error
Answer: Option A
Join The Discussion
Comments (1)
Related Questions on C Fundamentals
What is the primary purpose of a function prototype in C?
A. Declare a variable
B. Declare a function
C. Define a function
D. Assign a value
What is the correct syntax for declaring a variable in C?
A. int variable_name;
B. variable_name = 5;
C. variable_name int;
D. int = variable_name;

Initialization:int x = 2, y = 0; initializes x to 2 and y to 0.
Ternary Operation:The ternary operation int z = (y++) ? y == 1 && x : 0; evaluates as follows:
(y++): y++ increments y after its value is used in the expression. So, at this point:
The value of y++ is 0 (since y is initially 0).
After this operation, y becomes 1.
Since (y++) evaluates to 0 (which is false), the ternary operation will take the false branch, which is 0.