Thread: double vs int for statement

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    4

    double vs int for statement

    Here are two programs, in the first program i get d=0 as my answer in the statement, in the second program i get the correct answer d=7. Why does an incorrect answer appear in the statement when I am using the double command?

    In my google search something came up about doubles not being allowed for use in statements, but I couldn't find any other information on the topic. Thanks in advance for your responses.



    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        int a = 3, b = 4;
        double d;
    
    
        d = a + b;
        
        printf ("The value of d is %d\n", d);
        return (0);
    }


    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        int a = 3, b = 4, d;
    
    
        d = a + b;
        
        printf ("The value of d is %d\n", d);
        return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In the first example, d is a double, hence to print it, you should use %f, e.g.,
    Code:
    printf ("The value of d is %f\n", d);
    Other than that, the code looks fine since the implicit conversion from int to double is fine.
    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

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    In addition to Laserlight you can also type cast in the first program to get the correct answer.
    ex.)
    Code:
    printf ("The value of d is %d\n", (int)d);

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Josh Genser View Post
    In my google search something came up about doubles not being allowed for use in statements
    "doubles are not allowed for use in statements"?? I wonder what that could mean. The main problem with double is that it cannot precisely represent all integers, so a statement like

    d == SOME_INTEGER

    can evaluate to false, because the double d is "almost" equal to SOME_INTEGER but not close enough. Also, for integers, as long as the statements are defined, the following will always be true

    i+1 > i

    However, with a double, you will eventually get a large enough value such that

    d+1 > d

    evaluates to false, which means incrementing and decrementing doubles is not recommended.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    4
    Thanks for the input guys. One this I'm still confused is the %f and %lf commands. Are they supposed to be used for doubles like %d is supposed to be used for integers?

    Does anyone know of a list somewhere where someone can find the corresponding % command for a given variable?

    Camel-man, would using that (int) turn d into an integer?

    Sorry if these questions seem basic but I started programming less than a week ago.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Josh Genser View Post
    Does anyone know of a list somewhere where someone can find the corresponding % command for a given variable?
    They are mentioned in the standards as well as in the "man pages". If you don't have man pages installed, my favorite website for them is unix.com:

    Man Page for printf (posix Section 3) - The UNIX and Linux Forums

    Man Page for scanf (all Section 3) - The UNIX and Linux Forums

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Josh Genser View Post
    Camel-man, would using that (int) turn d into an integer?
    Only for the line that you cast.

    Example
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            double pi = 3.14;
    
            printf("%f\n", pi);
            printf("%d\n", (int)pi);
            printf("%f\n", pi);
    
            return 0;
    }
    Output:
    Code:
    3.140000
    3
    3.140000
    And of course you don't need to apologize.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-15-2012, 07:10 PM
  2. Double liked list and double pointer
    By saillesh.sabari in forum C Programming
    Replies: 1
    Last Post: 12-10-2010, 11:03 AM
  3. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  4. Using a double number in while statement
    By Daesom in forum C++ Programming
    Replies: 6
    Last Post: 10-31-2006, 06:51 PM
  5. Changing double time to double cost
    By chrismax2 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 10:29 AM