C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 7)
7.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int i=4, j=8;
    printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, i^j);
    return 0;
}
12, 12, 12
112, 1, 12
32, 1, 12
-64, 1, 12
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
58 comments Page 1 of 6.

Mukta said:   5 years ago
Thank you everyone for explaining the answer.

Swaroop said:   6 years ago
Thanks @Vedashree.

Sanket said:   6 years ago
Thanks @Vedhashree.

Laxman said:   7 years ago
In function data passing, expressions are evaluated from right to left,but printing results from left to right, so in the given problem first (i^j) evaluated,

i=0100
j=1000
i^j=1100 equivalent decimal is 12.

Then, next expression is (i|j&j|i)
the expression having bitwise AND and OR, in this bitwise AND having higher priority than bit wise OR, so grouping made as i|(j&j)|i, in this first (j&j) evaluated.

j=1000
j=1000
j&j=1000.

The remaining expression made as i|(1000)|i; here bitwise OR having two times that means same priority now we have to consider associativity. For bitwise operators left to right.

i=0100
1000
res=1100
1000

Final res=1100 decimal value 12.
third also same as the second expression so the result is 12.

Now printing the results in 12 12 12.
(12)

Ravi said:   7 years ago
Good explanation, Thanks. @Ramadas.
(1)

Harshada said:   7 years ago
Not understood. Please help me.

Ramya said:   8 years ago
Thanks @Vedhashree.

Manoja V. said:   8 years ago
Thanks @Vedhashree.

Anchan said:   8 years ago
Thanks @Vedhashree.

Chandru said:   8 years ago
Thanks @Vedhashree.


Post your comments here:

Your comments will be displayed after verification.