Thread: beginner question double data type

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    14

    beginner question double data type

    I am beginner in c programming, As far as I understand it is used to store decimal number and float number.

    Code:
    #include <stdio.h> int main() 
    { 
        int a = 1; 
        char b ='G'; 
        double c = 3.14; 
        printf("a = %d\n", a); 
        printf("b = %c\n", b);
        printf("c = %lf\n", c); 
      
        return 0; 
    }


    a = 1
    b = G
    c = 3.140000


    but there is already int type to store decimal and float to store float number so what's the specific use of double data type in C language ?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    717
    Quote Originally Posted by muke786 View Post
    but there is already int type to store decimal and float to store float number so what's the specific use of double data type in C language ?
    Precision: float has 24 bits of precision, double has 53 bits of precision, long double has 64 bits of precision.

    Anyway... your printf format argument is wrong, it should be "%f". "%Lf" (uppercase ell) is used to 'long double'.

    []s
    Fred

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,920
    Quote Originally Posted by flp1969
    your printf format argument is wrong, it should be "%f".
    It's superfluous, but then there's the argument that it is consistent with scanf.
    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

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    14
    I am not clear I do not understand when to use double data type ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,167
    Nowadays, use double all the time.

    floats are a memory space and performance vs range and precision tradeoff back when floating point was done in software (floats are easier to compute with than doubles), and when your entire memory was measured in 10's of KB rather than GB.

    Nowadays, most of the systems you'll come across will have floating point hardware and plenty of memory.
    Float vs double isn't a choice you need to agonise over.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-04-2012, 08:16 AM
  2. changing a string to a 'double' data type
    By Brian_Jones in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2009, 05:59 PM
  3. C - float and double data-type question..
    By ShadeS_07 in forum C Programming
    Replies: 10
    Last Post: 07-14-2008, 06:06 AM
  4. Beginner Question - Linking a data file
    By noiprocs2 in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2008, 08:58 AM
  5. Double Data Type
    By HallmanBilly in forum C Programming
    Replies: 2
    Last Post: 09-01-2007, 03:25 PM

Tags for this Thread