Like Tree2Likes
  • 2 Post By laserlight

Question from a Noobie

This is a discussion on Question from a Noobie within the C Programming forums, part of the General Programming Boards category; This is my first here - i love this website. I have been teaching myself C and have a very ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    99

    Question from a Noobie

    This is my first here - i love this website. I have been teaching myself C and have a very beginner question that is probably easily answerable. Why does the following program give me garbage for the "long" type value? Will any other data type besides "float" work? Why not? Thanks in advance.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
        int yearsold;
        float floatsecondsold;
        long longsecondsold;
        printf("Please enter how many years old you are!\n");
        scanf("%d", yearsold);
        floatsecondsold=yearsold*3.156e7;
        longsecondsold=yearsold*3.156e7;
        printf("According to float, you are %f seconds old.  According to long, you are %f seconds old.\n", floatsecondsold, longsecondsold);
        return 0;
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,389
    See red
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
        int yearsold;
        float floatsecondsold;
        long longsecondsold;
        printf("Please enter how many years old you are!\n");
        scanf("%d", &yearsold);
        floatsecondsold=yearsold*3.156e7;
        longsecondsold=yearsold*3.156e7;
        printf("According to float, you are %f seconds old.  According to long, you are %ld seconds old.\n", floatsecondsold, longsecondsold);
        return 0;
    }
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Thank you very much, but I swear something still fishy. See if you can figure out this one:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
        int quarts_water;
        int grams_of_water;
        float molecules_of_water;
        long longmolecules;
        printf("Please enter how many quarts of whater you have!\n");
        scanf("%d", &quarts_water);
        grams_of_water=quarts_water*950;
        molecules_of_water=grams_of_water/3e-23;
        longmolecules=grams_of_water/3e-23;
        printf("You have %f molecules of water, and according to long you have %ld molecules.\n", molecules_of_water, longmolecules);
        return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    What needs to be figured out?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    When I run this program, I get garbage for the long printout. I'm having a hard time substituting different data types, basically.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by gratiafide
    When I run this program, I get garbage for the long printout.
    One problem I see is that you are dividing by a number such that your result, after truncation, is likely to be outside of the range of long. A check shows that such a conversion from double to long results in undefined behaviour, hence the garbage that you observed.
    Salem and Click_here like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another noobie question
    By noobie in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 01:04 PM
  2. A third noobie question
    By Noobie in forum C++ Programming
    Replies: 5
    Last Post: 01-20-2003, 11:53 PM
  3. Noobie question
    By Noobie in forum C++ Programming
    Replies: 49
    Last Post: 01-20-2003, 04:31 PM
  4. noobie question!
    By tetra in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 01:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21