# Thread: Beginner problem with floats

1. ## Beginner problem with floats

Hi everyone,

I'm working through the Kochan book "Programming in C". I was doing fine following along until chapter 4, exercise 4. I'm trying to write a conversion program using C = ( F - 32 ) / 1.8 . Here's what I came up with:

Code:
```#include <stdio.h>

int main (void)

{
//declare variables

float f1;
float c1 = ( f1 - 32 ) / 1.8;

//print result

f1 = 27;
printf ("27 degrees Farenheit = %f degrees Celsius\n", c1);

return 0;
}```
My program returns −17.77, so I'm thinking maybe I declared the float variables wrong and it only reads -32/1.8 ? Any help would be appreciated, thanks.

2. 1) Float f1 is uninitialized, so it picks up some unknown value.
2) Then you calculate c1 using an unknown value.
3) THEN you assign "27" to f1!!!

Reverse Step 2 and Step 3 and recompile.

3. Code:
`    float c1 = ( f1 - 32 ) / 1.8;`
( f1 - 32 ) / 1.8 here is an expression that is evaluated - it's not a function like you'd write in math parlance. c1 doesn't get updated when you modify f1 in line 13, it gets its value at line 9 and that's it.
Like rstanley said - in line 9 c1 will have indeterminate value because f1 has indeterminate value. If you want a function to pass a value into and get a converted value out of, you need to... well, actually make a function. ; )
Code:
```float fahrenheit_to_celsi(float fahrenheit) {
// return converted stuff
}```
That's not _just_ indeterminate value though. This seems to be straight Undefined Behavior. Address of f1 is never taken, thus it could be declared with register keyword, thus this violates 6.3.2.1:
2 [...] If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undeﬁned.
So, consider yourself lucky that Chaos Demons didn't fly out of your monitor to eat your soul. ; )

4. Thanks to you both, I see what was wrong and it's all working well now.