C | Operators | Question 6
What is the output of below program?
#include <stdio.h> int foo(int* a, int* b) { int sum = *a + *b; *b = *a; return *a = sum - *b; } int main() { int i = 0, j = 1, k = 2, l; l = i++ || foo(&j;, &k;); printf("%d %d %d %d", i, j, k, l); return 0; } |
chevron_right
filter_none
(A) 1 2 1 1
(B) 1 1 2 1
(C) 1 2 2 1
(D) 1 2 2 2
Answer: (A)
Explanation: The control in the logical OR goes to the second expression only if the first expression results in FALSE. The function foo() is called because i++ returns 0(post-increment) after incrementing the value of i to 1. The foo() function actually swaps the values of two variables and returns the value of second parameter. So, values of variables j and k gets exchanged and OR expression evaluates to be TRUE.
Quiz of this Question
Recommended Posts:
- Operators in C | Set 2 (Relational and Logical Operators)
- C | Operators | Question 9
- C | Operators | Question 23
- C | Operators | Question 10
- C | Operators | Question 22
- C | Operators | Question 24
- C | Operators | Question 26
- C | Operators | Question 27
- C | Operators | Question 8
- C | Operators | Question 7
- C | Operators | Question 16
- C | Operators | Question 27
- C | Operators | Question 2
- C | Operators | Question 3
- C | Operators | Question 26



