Rajeshacme

When i execute the below test program

#include<stdio.h>

void main()
{
double l_double_value;

l_double_value = (double) (0xffffffff) ;

printf("%lx\n", (long) l_double_value);

}

I get the below output. in VC++ 8.0. 80000000. But in VC++ 6.0 i get fffff...

Can you pl help me to know about this..

Regards rajesh.



Re: Visual C++ General data type conversion in VC++ 8.0

Bite Qiu - MSFT

Hi Rajesh,

Please refer to section 6.3 Conversion of C Standard: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf,

"If the value being converted is outside the range of values that can be represented, the behavior is undefined."

For this case, you are casting a "double" to "signed long", the value of double type is "4294967295.0000000", this value is outside the range of values that can be represented by a signed long(from -2147483648 to 2147483648), so this behavior is underfined.

In this paticular case, the undefined behavior turns to have a result of -2147483648 (get from debugger), its binary representation is: 0x80000000, which is exactly the result you get from printf, so there is nothing wrong in VC++ 8.0. In VC++ 6, the cast expression may turns out to have another result(since it is undefined), so the output can be different from different compilers.

hope it helps

rico






Re: Visual C++ General data type conversion in VC++ 8.0

Rajeshacme

Dear rico,

Thank you for your reply. It is useful for me to analyze..

Regards,
Rajesh.