Thread: Arguments and type casting

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    Arguments and type casting

    I wanted to start learning C, and I just got to arguments and they are pretty useful and I decided to write a celsius to fahtrenheit program that took the celsius degree as an arg and typecast it to type long so I could do the calculation. I don't know what's wrong, but I always get a really big negative number. Here is the code:

    Code:
    1:      /**************************************
    2:       * cels2fahr.c:
    3:       * A program that takes an integer as an argument
    4:       * and converts it from Celsius (C) to Fahrenheit
    5:       * (F).
    6:       ************************************/
    7:
    8:      #include <stdio.h>
    9:
    10:     int main(int argc, char *argv[]) {
    11:
    12:             int fahr = 0;
    13:
    14:             if (argc < 2) {
    15:                     printf("Proper Usage: \n");
    16:                     printf("cels2fahr <DEGREES IN CELSIUS>\n\n");
    17:                     return 1;
    18:             }
    19:
    20:             fahr = (9.0/5.0) * (long) argv[1] + 32;
    21:
    22:             printf("%d\n", fahr);
    23:
    24:             return 0;
    25:     }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Arguments are charactor arrays, you can't just type cast them and have them changed to a numeric representation for you. There is the atoi function that accepts a char pointer and returns it's numeric value.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Did you actually try to compile the code with the line numbers in? That won't work.

    atoi() . . . sscanf() is better. See the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    2
    No, I wrote a program that lists it out like that for me and I copied/pasted it onto here. Ok, so I need to use atoi()? I will get right on that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. c program error
    By cutelucks in forum C Programming
    Replies: 14
    Last Post: 12-21-2007, 11:12 PM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM