Skip to content
Related Articles

Related Articles

Improve Article

C | Operators | Question 6

  • Difficulty Level : Easy
  • Last Updated : 28 Jun, 2021
Geek Week

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;

(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

Take a step-up from those “Hello World” programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
My Personal Notes arrow_drop_up
Recommended Articles
Page :