Difference in datatypes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Difference in datatypes

#include <stdio.h> int main() { const double pi=3.14; const float PI=3.14; printf("In case of double : %f \n",pi); printf("In case of float : %f \n",PI); return 0; } What exactly is the difference between double and float?(I know the theoretical answer,but they both are giving the same results.Please give me a code that will make the difference between the two clear.)(Something that only double can do and float cannot and vice-versa)

29th Jun 2019, 12:01 PM
M∆π∆$
M∆π∆$ - avatar
5 Answers
+ 7
/* Manas I had same question a week ago . Almost everyone knows theorotically that double has more precision than float. But what does it really mean? It means if you assign a number with many digits after decimal point to them both will have different precision. float can handle this to certain level and double can even more. Simply printing them with printf gives same output . But just try to format the output with dot (.) After % sign of format specifier you'll see the difference */ #include <stdio.h> int main() { float PIf=3.14265686494279; double PId=3.14265686494279; //same value set to both printf("float =%.14f\n double=%.14lf",PIf,PId); return 0; } /*outputs are different ! float looses precision on 6 th digit but double holds value upto 14 digits after decimal! */
29th Jun 2019, 12:21 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 7
Using double as your data type is more precise then using float. With double you can do something like... double x = 1.23456789; //ok float y = 1.23456789f; //not ok you will lose precision.
29th Jun 2019, 12:16 PM
D_Stark
D_Stark - avatar
+ 5
The Double, and Float variable types are different in the way that they store the values. Precision is the main difference where float is a single precision (32 bit) floating point data type, double is a double precision (64 bit) floating point data type. Float - 32 bit (7 digits) Double - 64 bit (15-16 digits)
29th Jun 2019, 12:15 PM
Asmit joy
Asmit joy - avatar
+ 1
Then you know that the only difference is their size. Just like long int, int, short, and char. All of them are the same and only differ in size. Since double is larger than float, it only means that it can be more precise than float
29th Jun 2019, 12:13 PM
Agent_I
Agent_I - avatar
0
Float - 32 bit (7 digits) Double - 64 bit (15-16 digits)
20th Aug 2020, 8:22 AM
Mahmoud Fadl
Mahmoud Fadl - avatar