Adam Lee
Adam Lee

Reputation: 3003

Returning a 1.0f gives me 1065353216

I have a C function that returns a type float.

When the function returns 1.0f, the receiver sees 1065353216, not 1.0.

What I mean is, the following:

float Function()
{
    return 1.0f;
}

float value;
value = Function(); 
fprintf(stderr, "Printing 1.0f: %f", value);

Displays:

1065353216

But not:

1.0

Upvotes: 5

Views: 2950

Answers (3)

unkulunkulu
unkulunkulu

Reputation: 11922

You define your function in one source file and call it from another one not providing the signature making the compiler think that the signature is int Function(), which leads to strange results.

You should add the signature: float Function(); in the file where the printf is.

For example:

float Function();
float value;
value = Function(); 
fprintf(stderr, "Printing 1.0f: %f", value);

Upvotes: 15

Simon
Simon

Reputation: 32933

My turn to guess the problem:

You are either:

  • editing the source code without saving it
  • editing one file but compiling and running another one
  • editing and compiling one file but running another one
  • doing something even more complicated but similar :)

Upvotes: 0

Yann Ramin
Yann Ramin

Reputation: 33197

Double check your work, as your implementation is correct.

Evidence: http://codepad.org/QlHLEXPl

Upvotes: 4

Related Questions