C double data type

    In C double is a keyword for the double data type. It represents floating point numbers with better precision. Usually we associate the name with the idea that it has double the precision of the float type.

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!

C double - declaring variables

Declaring a variable is just like with any other type:

    double price = 9.95;

Printing uses the format specifier %lf (%lg, %le or %la are equivalent). This is the only correct way for compilers that comply with С99 or later. Anyway, many compilers still use the ANSI way and allow printing doubles with the old %f.

    printf("price: %lf", price);

Reading with scanf is done with %lf with all compilers.

    double salary;
    scanf("%lf", &salary);
    printf("salary: %lf", salary);

Representation of double in C

    In C double’s exact precision depends on the implementation. Most compilers today use the IEEE-754 standard. To represent the numbers they use 64 bits as follows:

  • The first bit stands for the sign. 1 means negative, 0 means positive
  • 52 bits of significand (mantissa)
  • 11 bits of exponent

    It can keep up to about 16 digits before the variable is overflowed. For instance we can keep the pi number with a precision of 15 digits after the decimal point:

    double pi = 3.141592653589793;

    If we try to keep too much data in a variable, information will be lost. The information will be lost right to left.

    double bigNumber = 9876543210.123456789012345;
    printf("big num: %f\n", bigNumber);

     This will print 9876543210.123457 for a total of 16 digits, where the last is rounded up to 7.

Normally the range and precision of double is more than enough in practice.

See also:

Previous keyworddo..while

Next keywordelse

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!


› double

Did this help? Support me with your vote ;-)

Did this help?