Thread: Beginner problem with floats

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    2

    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. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    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. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    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 undefined.
    So, consider yourself lucky that Chaos Demons didn't fly out of your monitor to eat your soul. ; )

  4. #4
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    Thanks to you both, I see what was wrong and it's all working well now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A beginner's problem !
    By Coca cola in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2011, 03:08 AM
  2. Problem when qsorting an array of floats
    By avi2886 in forum C Programming
    Replies: 8
    Last Post: 04-30-2009, 02:46 PM
  3. problem with floats
    By echo49 in forum C Programming
    Replies: 2
    Last Post: 01-23-2007, 07:41 PM
  4. problem with floats in conditional statements
    By Desolation in forum C Programming
    Replies: 3
    Last Post: 07-08-2006, 01:56 AM

Tags for this Thread