What is the difference between float and double in C

1 Answer

0 votes
#include <stdio.h>

// double has 2x the precision of float. 
// double has 15 decimal digits of precision, while float has 7.

void float_check() {
    float a = 0.01234f;
    float b = 0;
    
    for (int i = 0; i < 813; i++) {
        b += a;
    }
    
    printf("%.7g\n", b);
}

void double_check() {
    double a = 0.01234;
    double b = 0;
    
    for (int i = 0; i < 813; i++) {
        b += a;
    }
    
    printf("%.15g\n", b); 
}

int main() {
    float_check();
    double_check();
    
    return 0;
}

// calculator = 10.03242
 
/*
run:

10.03238
10.03242

*/
 

 



answered Nov 20, 2024 by avibootz

Related questions

1 answer 143 views
1 answer 136 views
1 answer 66 views
1 answer 58 views
...