Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Diodor

Yet another printf question.

This topic is 6087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
To get the answers you seek simply add this statement into your program and marvel at the beauty.

printf("\t\t\b\b\b");

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!