Explanation of guess the output questions posted on our Instagram Page.
Click here to view Guess The Output series 5.
1) Guess the output
Output of above C Program is A) 0 10.
The final value of integer variable z is 0 and variable x is 10.
Why 🤔 ?
Let’s understand logical AND operator (&&).
It(&&) returns true
if both operands are true
otherwise it returns false
.
Initially, first operand is completely evaluated and if first operand evaluates to true i.e.(non-zero) then and then second operand will be evaluated.
In this case, the first operand is x>y and second operand is x++.
Result of first operand(x>y) is zero because value of x is 10 and y is 20. 10>20 is false.
So the second operand (x++) will not be evaluated and value of x remains same.
The result of x>y && x++ is assigned to integer variable z and which is 0(false).
Finally value of z is 0 and x is 10 so the output of above C Program is A) 0 10.
Click on Execute button to Run above code:
2) Guess the output
Output of above C Program is C) 1 20.
The final value of integer variable z is 1 and variable y is 20.
Why 🤔 ?
Let’s understand logical OR operator (||).
It(||) returns true
if one of the operand is true
. If both operands are false
then it returns false
.
Initially, first operand is completely evaluated and if first operand evaluates to true i.e.(non-zero) then the second operand won’t evaluated. If first operand evaluates to false i.e.(zero) then the second operand will be evaluated.
In this case, the first operand is y>x and second operand is y++.
Result of first operand(y>x) is one(non-zero) because value of y is 20 and x is 10. 20>10 is true.
So the second operand (y++) will not be evaluated and value of y remains same.
The result of y>x && y++ is assigned to integer variable z and which is 1(true).
Finally value of z is 1 and y is 20 so the output of above C Program is C) 1 20.
Click on Execute button to Run above code:
3) Guess the output
Output of above C Program is B) 20 .
The final value of integer variable x is 20 and variable a is 10.
Why 🤔 ?
Let’s understand the precedence order of operators in C .
1) ++ or — (Postfix)
2) ++ or — (Prefix)
3) * , / , %
4) +, –
Precedence order of Postfix operator is more so a– has higher precedence. The statement a-- + ++a
will execute from left to right.
Post decrement/increment(a–/a++) means it will first assign the value and then decrement/increment it by 1.
Pre decrement/increment(–a/++a) means it will first decrement/increment the value by 1 and then assign it.
That’s why the output of above C Code is 20.
If you have any question regarding this C Program, DM us on Instagram.
Click on Execute button to Run above code:
4) Guess the output
Output of above C Program is C) 0 .
The final value of integer variable x is 0 and variable a is 11.
Why 🤔 ?
Let’s understand the precedence order of operators in C .
1) ++ or — (Postfix)
2) ++ or — (Prefix)
3) * , / , %
4) +,-
We know the simple rules of mathematics
a – + a = a – a
a + – a = a – a
So a - + ++a
becomes a - ++a
.
Here precedence of ++a
is higher and statement will execute from right to left.
The statement will look like thisx = 11 - 11
(a – ++a)x =
0
Final value of x is 0 and a is 11.
Click on Execute button to Run above code:
5) Guess the output
Output of the above C Program is C) 2.
Value of variable m is 1 initially. And we never call the f1() function in the main() function then how the value of variable m becomes 2.
Here is the answer.
__attribute__((constructor)) is used with a function, which executes before main() function i.e. at the start of the program.
So the function f1() is called before main() and the final value of m is 2.
Click here for the more info about __attribute__((constructor)).
Click on Execute button to Run above code:
For more programming related questions and fun,
Follow us on Instagram.
https://www.instagram.com/computer_science_engineering_/
Click here to view Guess The Output series 5.