Thread: Problem with printing double numbers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    132

    Problem with printing double numbers

    Hi, everyone. I am trying to use the code below to print the numbers 2, 2 + 0.1, 2 + 0.1 + 0.1, and so on, performing the loop 1000 times, but somehow the printing starts 72.6 and not 2, why?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        double number = 2.0;
        int i;
    
        for ( i = 1 ; i <= 1002 ; i++ )
        {
            printf("%f\n", number);
            number = number + 0.1;
        }
    
        return 0;
    }
    Thanks in advance!

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Use %lf for double, %f is for floating point numbers

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Thanks. I tried that, but it didn't work. I believe %lf is used for scanf, but not for printf.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    use %lf and if you put a getchar() at beginning of for loop you will see that it does start at 2.0 but it eventually out grows cmd line space For some reason, Someone will know why I didnt know that you couldnt see everything from the beginning but apparently there is a limit

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Thanks, I hadn't realized the first results were being left behind in the console window, and the step to be added to variable number should be 1, and not 0.1.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    The code you posted works just fine on my machine, it starts at 2.0 and goes all the way up to 102.1. Did you edit/fix it?

    > Use %lf for double, %f is for floating point numbers

    Wrong, use %f. Scanf() teaches horrible habits, this confusion being one of them.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Hello, memcpy. I had to edit it in the following way, so that I could print the numbers from 2 to 1002 using a double variable:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        double number = 2.0;
        int i;
    
        for ( i = 1 ; i < 1002 ; i++ )
        {
            printf("%f\n", number);
            number = number + 1.0;
        }
    
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,908
    Quote Originally Posted by memcpy View Post
    Wrong, use %f. Scanf() teaches horrible habits, this confusion being one of them.
    Actually, it's printf that teaches bad habits, or at least causes confusion. Scanf needs to know how much space each modifier can fill. It makes sure you're honest, and it removes the confusion by making %lf and %f different, since they are for doubles and floats (definitely different types), respectively. A float, "%f", typically requires 4 bytes. A double, "%lf", typically requires 8 bytes. The difference is very significant as the internal representation of the two in memory are not interchangeable. printf, on the other hand, doesn't care. The reason for this is that in C, floating point parameters are automatically promoted to a double when passed to a function. Thus, a float passed to printf gets turned into it's equivalent double representation by the compiler. That means that a "%f" for a float parameter and "%lf" for a double parameter both end up being processed as a double inside printf, regardless of %f or %lf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Double?
    By IKnew in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2010, 03:41 PM
  2. Reading from file and printing double data
    By hammari in forum C Programming
    Replies: 4
    Last Post: 07-14-2009, 07:02 AM
  3. Printing a double forwardslash...?
    By fraktal in forum C Programming
    Replies: 11
    Last Post: 01-18-2006, 04:10 PM
  4. double numbers
    By Gil22 in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2003, 10:12 PM
  5. Problem Formatting double numbers.
    By chaps67 in forum C++ Programming
    Replies: 1
    Last Post: 09-19-2001, 09:20 AM