Tell me the output of the following code with explanation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Tell me the output of the following code with explanation

(skipping necessary parts like #include etc.) int a=10,b=3,c; c=(float)a/b; cout《c;

11th Jul 2016, 10:55 AM
Ishan saini
Ishan saini - avatar
3 Answers
+ 1
It will devide 10 by 3 .
11th Jul 2016, 11:24 AM
Aman Singh
+ 1
First you are creating three variables(a, b ,c) of type int, and assigning 10 and 3 to them respectively. Note that you are not initializing c to any value. This is not bad when you are assigning a value to it later on, before using it. As of the second statement, first the right hand side (rhs) of the statement is evaluated: a is casted into a float, and that float value is divided by b, so 10.0f / 3, which evaluates to 3.333.... This rhs value is then assigned to c, but note that c is an int so it is going to be the truncated version of the rhs expression (a truncated number is a decimal number without the decimal part), which is 3, so the value of c is 3 now. The last statement outputs the value of c (3) into the output(most of the times the terminal).
11th Jul 2016, 11:31 AM
Garme Kain
Garme Kain - avatar
0
It will simply divide 10 by 3 and print c i.e 3.3. But its better to assign a,b,c as float directly than assigning it as int and then as float. GoodLuck
11th Jul 2016, 4:39 PM
Rishan Mascarenhas
Rishan Mascarenhas - avatar