Thread: Fahrenheit to Celsius Converter

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Fahrenheit to Celsius Converter

    Hello!
    I'm studying C for some exams I have
    In my book I'm writing exactly the code it provides and I believe it's wrong somehow

    Code:
    #include <stdio.h>
    void main(int argc, char **argv)
    {
        double f , c;
        printf("Give Temperature in Fahreneit:");
        scanf("%g" , &f);
        c = 5.0 * (f-32) / 9.0;
        printf("Celcious Degrees: %g" , c);
        return 0;
    }
    I'm using Codeblocks, the suggested C compiler from the website

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The first problem is main should be defined as returning an int. Secondly your scanf format string is using the %g specifier which is for a float, you need the long specifier %lg for a double.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Why do you believe it's wrong? The calcs look reasonable. Did you test against some known conversions (e.g. 32F = 0C)? Are you getting error messages? Next time, you should post the error messages with line numbers:
    Code:
    $ gcc -Wall temp.c
    temp.c:2: warning: return type of ‘main’ is not ‘int’
    temp.c: In function ‘main’:
    temp.c:6: warning: format ‘%g’ expects type ‘float *’, but argument 2 has type ‘double *’
    temp.c:9: warning: ‘return’ with a value, in function returning void
    Problems that my compiler told me about:
    1. main shouldn't be a void function. it's int main(void). Read why it matters here: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]).
    2. You can't return an int from a void function (which is how you declared main). This will go away once you fix the definition of main.
    3. Read the docs for scanf. You need to use %lg for a double (that ell tells you double, not float).

    EDIT: Dang you jimblumberg and your speedy fingers!

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Alright I changed "%g" to "%lg" and the converter works but I get "Process terminated with status 21 (0 minutes, 6 seconds)"
    What do I need to change?

    Code:
    #include <stdio.h>
    void main(int argc, char **argv)
    {
        double f , c;
        printf("Give Temperature in Fahreneit:");
        scanf("%lg" , &f);
        c = 5.0 * (f-32) / 9.0;
        printf("Celcious Degrees: %lg" , c);
    }
    Woah thanks a lot I changed void main to int main (int argc, char *argv[] ) and added return 0 in the end and now it works flawlessly
    Last edited by MariosX; 09-02-2011 at 03:28 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to do the other changes also mentioned above. I suppose it wouldn't hurt to spell Celsius and Fahrenheit correctly while you're at it.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by anduril462 View Post
    1. main shouldn't be a void function. it's int main(void). Read why it matters here: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]).
    You didn't read the link I posted, or Jim's or my comments very closely. Click on that red text. It's a link. Read about the proper way to declare main and why it matters. That article also links you to a great discussion of exactly why your program did what it did.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Yeah I had to change the second line to: int main (int argc, char *argv[] )

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by MariosX View Post
    Yeah I had to change the second line to: int main (int argc, char *argv[] )
    And what did you put as the very last line of main? I'm guessing you still get warnings when you compile, and it should tell you exactly the problem. Note that, since your program doesn't take command line arguments, you should probably do int main(void). Make your code match your intentions and be explicit.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by anduril462 View Post
    And what did you put as the very last line of main? I'm guessing you still get warnings when you compile, and it should tell you exactly the problem. Note that, since your program doesn't take command line arguments, you should probably do int main(void). Make your code match your intentions and be explicit.
    In the last line of the program I added "return 0;"
    I get no warning nor errors I get this "Process terminated with status 0 (0 minutes, 3 seconds)"
    I read that if the process is terminated with status 0. It means it is ok unless I'm mistaken.
    I'm a newbie in C, first day using it on my own.

    Code:
    #include <stdio.h>
    int main (int argc, char *argv[] )
    {
        double f , c;
        printf("Give Temperature in Fahreneit:");
        scanf("%lg" , &f);
        c = 5.0 * (f-32) / 9.0;
        printf("Celcious Degrees: %lg" , c);
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Excellent. Just wanted to make sure, since you didn't mention it. You're off to a good start, so keep it up and come back if you need more help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Celsius in Fahrenheit converter - does not run
    By ApeWithGrape in forum C Programming
    Replies: 10
    Last Post: 03-06-2011, 11:01 PM
  2. Please help converting Fahrenheit to Celsius
    By victory1 in forum C Programming
    Replies: 16
    Last Post: 10-08-2009, 09:08 AM
  3. Replies: 8
    Last Post: 09-25-2009, 11:15 AM
  4. Conversion from Fahrenheit to Celsius problem
    By -Syntax in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2008, 08:56 PM
  5. celsius to fahrenheit conversion
    By chipster18 in forum C Programming
    Replies: 11
    Last Post: 03-26-2003, 07:38 AM