+ 1

Float x=5; int y=x; auto v=x-y;

what is the data type of v(int or float)?

15th Aug 2017, 12:14 PM
Mukul
Mukul - avatar
6 odpowiedzi
+ 9
Data Type of v is float. ## This is a result of the pattern called usual arithmetic conversions that converts operands in order to yield result in a common type : if either operand is float, the other shall be converted to float. the only case where the type of result is int, is when both operands are int.
15th Aug 2017, 12:35 PM
MizoPro
MizoPro - avatar
+ 1
thank u..😁👍
15th Aug 2017, 12:55 PM
Mukul
Mukul - avatar
+ 1
Tip: If you are not sure what data type something is here is a trick template<typename T> class NonExistingClass; int main() { float x = 5; int y = x; auto v = x - y; NonExistingClass<decltype(v)> a; } You declare a non existing template class when you attempt the run this code the compiler should give you an error message along the lines of: error: aggregate 'NonExistingClass<float> a' has incomplete type and cannot be defined. and now you know v is a float :)
15th Aug 2017, 1:45 PM
Dennis
Dennis - avatar
0
float is decimal value like 1.2,2.3 etcc how could u give integer value in float
15th Aug 2017, 12:33 PM
corejava
0
that a PrO TriCK Dude.....
15th Aug 2017, 2:25 PM
Mukul
Mukul - avatar
- 1
1) declairing float x=5 a) printing %f x--> 5.00000 b) printing %d x---> 0 2)declaring int x= 5 a) printing %f a---> wont run in compilers....some compiler will show errors...some will compile but wont run
15th Aug 2017, 12:52 PM
sayan chandra
sayan chandra - avatar