[net.unix-wizards] Why won't this C program output the correct value?

trb (11/08/82)

I just got a glossy flyer from Computing Technology Group - Telemedia
("our business is UNIX training").  The entire point of the flyer was
to advertise their UNIX training courses.  They used an ACTUAL C CODE
example in their ad, enticing to us hacker types.  Here it is:

-----
Why won't this "C" program output the correct value?

main()
{
	float f = 2.5;
	printf("%d\n",f);
}
-----

Not intuitively obvious to the most casual observer; a good example,
right?  They go on to explain about different types, casting, and how C
is a type-oriented language, etc...  Now, let me quote further:

-----
The recommended solution to the printf "problem" is:

main()
{
	float f = 2.5;
	printf("%d\n",(int)f);
}
-----

If I were a beginner, I would be as puzzled by the output of the
second piece of code as I would be by the first.

The above seems to exemplify the mentality of a certain
portion of the consulting populace, who are more interested
in raking in the big bucks than in doing a proper job.
Feh.

	Andy Tannenbaum   Bell Labs  Whippany, NJ   (201) 386-6491