From: JonY <jo...@us...> - 2012-08-26 00:57:37
|
On 8/26/2012 02:21, Dipanjan Das wrote: > On 25 August 2012 23:44, Keith Marshall <kei...@us... >> wrote: > >> On 25/08/12 18:54, JonY wrote: >>> On 8/26/2012 01:37, Dipanjan Das wrote: >>>> Please consider the snippet below: >>>> >>>> *#include <stdio.h>* >>>> * >>>> * >>>> *int main()* >>>> *{ * >>>> * printf("%f\n",1);* >>>> * printf("%f\n",(float)1.0);* >>>> * printf("%f\n",(int)1.0);* >>>> * printf("%f\n",1.0);* >>>> * printf("%f\n",1);* >>>> *} * >>>> >>>> ... snip ... >>>> >>> >>> You are writing code with undefined behavior, bad code is bad code, only >>> the 2nd printf is valid. >> >> > > >> Actually, both the 2nd and the 4th are valid; both pass an argument >> which is promoted to type "double", (a 64-bit entity). The remaining >> three pass an argument of type "int", which is only 32 bits, but the >> "%f" format specifier expects, and consumes 64 bits regardless. When >> you pass only 32 bits, the other 32 required could represent anything at >> all -- hence behaviour is undefined. JonY is right; your code is wrong, >> and you can have no expectation of consistent results. >> >> > But, on Turbo C compiler, the first line produces "Floating point format > not linked" error. I have not found the reason of this behavior though I > have come across it many a times. > It is part of the reason why your code is undefined, it does not conform to C standards. Some compilers may warn, others not, effects are entirely up to the compiler, OS and hardware combination. |