Thread: Question from a Noobie

  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,907
    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
    28,413
    What needs to be figured out?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02:04 PM
  2. A third noobie question
    By Noobie in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2003, 12:53 AM
  3. Noobie question
    By Noobie in forum C++ Programming
    Replies: 49
    Last Post: 01-20-2003, 05:31 PM
  4. noobie question!
    By tetra in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 02:58 PM