Thread: Beginner Q:)

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Beginner Q:)


    I have what should be a rather simple beginning c program to convert celsius to Farenh. below.

    the file will compile but won't 'build' for some reason. Any suggestions?
    thx
    _____________________________

    #include <stdio.h>
    /*Print Farenheit table*/
    Main()
    {
    int fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;
    while (fahr <= upper) {
    celsius = 5 * (fahr-32) / 9;
    printf("%d\t%d\n", fahr, celsius);
    fahr = fahr + step;
    }
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    You left out the main function. Every C program requires a main function.

    You should realize by now that C is case-sensitive, which means that if you change the case of any character in a variable, type, or function name, then that is a different identifier. Therefore, Main is something entirely different from main.

    PS
    This line,
    celsius = 5 * (fahr-32) / 9;
    will not do what you think it does. You are expecting it to perform floating-point division, but since both the dividend and divisor are integers it will perform integer division instead.

    For example,
    5.0/9.0 = 0.55555556
    but
    5/9 = 0

    Changing your expression to dividing by 9.0 should be enough to invoke floating-point division. But then you'll be assigning a double to an int which will raise a warning about losing accuracy. You can truncate the result by casting it to int, or you could round off by adding 0.5 before you cast it to int.
    Last edited by dwise1_aol; July 5th, 2013 at 03:09 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Cool,
    thank you :)

IMN logo majestic logo threadwatch logo seochat tools logo