Yet another printf question.

Started by
33 comments, last by Diodor 22 years, 4 months ago
I run this little program:
      
#include "stdafx.h"
#include <stdio.h>


int main(int argc, char* argv[])
{
	float a = 0.5f;
	int *b = (int*)&a
	printf ("%f %f\n", a, *b);
	return 0;
}
      
And on the console I get "0.5000000 0.0000000". What am I doing wrong? Why don't I get an "0.5000 0.5000"? Does my VC++ have a bug or did I missunderstand functions with ... arguments? All of this is a debug compile of a Win32 console app, no optimisations. Edited by - Diodor on November 16, 2001 11:07:56 AM
Advertisement
Since b is an int pointer, *b is an int. This is causing *b to be rounded down. Make b a float pointer and you''ll get what you expect.
Or typecast the int in the printf function to type float by using:

printf("%f %f\n", (float)a, (float)*b);

if you require ints

ICQ: 130925152
Email: e.j.folkertsma@student.utwente.nl
ICQ: 130925152Email: e.j.folkertsma@student.utwente.nl
quote:Original post by Dark
Or typecast the int in the printf function to type float by using:

printf("%f %f\n", (float)a, (float)*b);

if you require ints

ICQ: 130925152
Email: e.j.folkertsma@student.utwente.nl


Did you actually try that before you posted it? ''Cause it doesn''t work.
Maybe you could try this:

*(float *)b
nickm: that does work. "*((float *)b)" to be formal.

what''s really wierd is if you type the line "printf("%f %f\n", (*b), f);" the display is "0.000 0.000". i even tried with "long" instead of "int".

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
quote:
i even tried with "long" instead of "int".




Isn't long an int anyway?

Newbie to game programming?
visit

KaBooMgames
Great for Newbs, Masters welcome as well

Edited by - pointguard1 on November 15, 2001 6:43:17 PM
------------------------------------KaBeeM Web
<><
To get the answers you seek simply add this statement into your program and marvel at the beauty.

printf("\t\t\b\b\b");
ANSI C specifies that a "long" must be at least 32 bits. an "int" is usually the bit range of the processor (i.e. Pentium = 32-bit processor). back in the "286" days, most compilers treated an "int" as 16-bit.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Uh...

Not to spoil your fun or anything... but int''s don''t store values past the decimal place. AFAIK they just ignore it.

So your program is working correctly.

This topic is closed to new replies.

Advertisement