c program: what should be the answer given by this code? I typed the code into t
ID: 3665049 • Letter: C
Question
c program: what should be the answer given by this code?
I typed the code into the program and got an output of 1, the teacher said this is not correct.
Two more things to clarify please: 1) How is % used? Ex. if I have 4%1 will the answer be 4 or Zero, since the remainder is ZERO?
2) Difference between ++i and i++???
Num = 2;
Int i =2;
Int result = ++i - --i / 2;
Printf(“result = %d, i = %d”);
Return 0
#include #include int main) float num; num 2 ; int i = 2; int result = ++i i , 2; printf( "result %d, i %d", result, i); return 0Explanation / Answer
The answer for your program result =2 and i = 2
Int i =2 -- Thi initial value of i = 2;
Int result = ++i - --i / 2;
Here i is pre-incremented and post-incremented. (++i) where i = 2 becomes 3 and (--i), where i =3 becomes 2
result = 3 - 2/2 = 3-1 = 2
So Result = 2 and i = 2
Printf(“result = %d, i = %d”);
1) % - gives reminder, 4 % 1 gives reminder zero (0)
2) ++i is pre increment. The value of i is incremented and the statement executed, i++ means post-increment. In post increment i is incremented after the statement execution
For example assume x = ++i, i = 5,
i is incremented first , then i = 6 and it assigned to x = i = 6
now assume x = i++ and i = 5
Here i is incremented after the assignment, so x = 5 and i = 6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.