Page 1 of 1
Help for this exam paper Q
Posted: Thu Jul 28, 2011 11:16 am
by Face

- 1.JPG (39.78 KiB) Viewed 4649 times
I cant get understand what is the error of this.I found the answer,

But can't understand the error.
Re: Help for this exam paper Q
Posted: Thu Jul 28, 2011 11:52 am
by Herath
value is defined a short type variable. But 2 is "int" by default. The error (warning) is loss of precision. Because "int" is 32bit of size. The result of (value*2) is going to be int as I think. Then has to be converted to short in order to be assigned to the "value". So the value has to be cast manually.
possible fixes for this are
Code: Select all
value=(short) ((short) value * (short) 2);
But you can always cast a 16bit data type in to a 32bit or higher size type without any problem.
Re: Help for this exam paper Q
Posted: Thu Jul 28, 2011 1:06 pm
by Face
thanks brother.!