+ 6

Can any1 exaplain me the output?

include <stdio.h> int main() { int i = 3; int l = i / -2; int k = i % -2; printf("%d %d\n", l, k); return 0; }

28th Jul 2019, 1:19 PM
GameHuB
GameHuB - avatar
5 Answers
+ 55
GameHuB Then the value of k = -1; % is modulus operator which gives us remainder Here -- k = i % 2; means - k = -3 % 2; (% gives remainder -1) So , k = -1;
29th Jul 2019, 8:35 PM
Vinesh Wadhwani
Vinesh Wadhwani - avatar
+ 3
In C, integer division will result an integer, so the output is -1 1. C uses a truncated division where the result will be rounded toward zero to get the integer. So 3 / -2 is -1.5, the result is truncated and becomes -1, the second operator is modulo operator, which calculate the remainder. The quotient from 3 / -2 is -1, so the remainder is 1
28th Jul 2019, 1:59 PM
Agent_I
Agent_I - avatar
+ 2
GameHuB The value of k variable is -1, -3 / 2 is -1.5, the truncated quotient is then -1, so the remainder is -1
28th Jul 2019, 3:46 PM
Agent_I
Agent_I - avatar
+ 2
GameHuB k = - 1
20th Aug 2019, 8:10 PM
UraL
+ 1
if i write int i = -3; int k = i % 2; Then???
28th Jul 2019, 3:41 PM
GameHuB
GameHuB - avatar