C | Operators | Question 10
What is the output of following program?
#include <stdio.h> int main() { int a = 1; int b = 1; int c = a || --b; int d = a-- && --b; printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d); return 0; } |
chevron_right
filter_none
(A) a = 0, b = 1, c = 1, d = 0
(B) a = 0, b = 0, c = 1, d = 0
(C) a = 1, b = 1, c = 1, d = 1
(D) a = 0, b = 0, c = 0, d = 0
Answer: (B)
Explanation: Let us understand the execution line by line.
Initial values of a and b are 1.
// Since a is 1, the expression --b is not executed because // of the short-circuit property of logical or operator // So c becomes 1, a and b remain 1 int c = a || --b; // The post decrement operator -- returns the old value in current expression // and then updates the value. So the value of expression --a is 1. Since the // first operand of logical and is 1, shortcircuiting doesn't happen here. So // the expression --b is executed and --b returns 0 because it is pre-increment. // The values of a and b become 0, and the value of d also becomes 0. int d = a-- && --b;
Recommended Posts:
- Operators in C | Set 2 (Relational and Logical Operators)
- C | Operators | Question 2
- C | Operators | Question 6
- C | Operators | Question 5
- C | Operators | Question 16
- C | Operators | Question 26
- C | Operators | Question 18
- C | Operators | Question 22
- C | Operators | Question 17
- C | Operators | Question 15
- C | Operators | Question 23
- C | Operators | Question 7
- C | Operators | Question 11
- C | Operators | Question 12
- C | Operators | Question 13


