+ 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; }
5 Respostas
+ 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;
+ 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
+ 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
+ 2
GameHuB k = - 1
+ 1
if i write
int i = -3;
int k = i % 2;
Then???