How can this programme give this output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How can this programme give this output?

#include <stdio.h> int main() { int i = 2, j = 3, l ; l = j / i * i ; printf( "%d", l) ; return 0; Why does this code return l as 2? Like, how could it possibly ?

18th Oct 2020, 8:07 PM
Mia
Mia - avatar
4 Answers
+ 10
remember that if you work with integers, you will get integers as a result, the decimals will be ignored. You can also try different results by changing the order of the operation using parentheses, such as: l = j / (i * i); // Result: 0
18th Oct 2020, 8:18 PM
Mariano Fregosi
Mariano Fregosi - avatar
+ 7
3/2 = 1 1*2 = 2
18th Oct 2020, 8:12 PM
Mariano Fregosi
Mariano Fregosi - avatar
+ 4
Yeah I get it now, thanks so much
18th Oct 2020, 8:19 PM
Mia
Mia - avatar
+ 3
Ohhhh, thanks
18th Oct 2020, 8:13 PM
Mia
Mia - avatar