There are four data type in c language int, chat, float and double. I can understand when to use first three data type but double data type I can't figure out when to use it.
how do you use it?
There are four data type in c language int, chat, float and double. I can understand when to use first three data type but double data type I can't figure out when to use it.
how do you use it?
There are a lot of different types:
integer (char, short, int, long, long long; signed, unsigned)
floating point (float, double, long double)
complex (float, double, long double)
pointer
array
structure
union
enumeration
function
atomic
void
Ordinary language is totally unsuited for expressing what physics really asserts.
Only mathematics can say as little as the physicist means to say. - Bertrand Russell
Please read my response in another thread, and follow my recommendations. I have posted this several times over the years.
float usually has less precision and range than double. C uses double for all floating-point arithmetic (even if both operands of an operator are float). Use double to prevent truncating results to float.
That's not true.C uses double for all floating-point arithmetic (even if both operands of an operator are float).
Ordinary language is totally unsuited for expressing what physics really asserts.
Only mathematics can say as little as the physicist means to say. - Bertrand Russell
Oh no, you're right! I just looked up the C99 standard (later versions are probably the same on this particular matter), and an arithmetic operation on two float values results in a float type result. I must have been thinking of the default type of a floating-point literal (e.g., 1.0) being double, unless you append f or F to it.
To be frank, I haven't done much with floating point for years (I develop embedded software where floating point is rarely used).
I think it might have been true in the original version of C. Also, when a float is passed to a function that accepts a variable number of arguments such as printf, it's converted to a double. That's why %f and %lf are interchangeable for printf. (Not for scanf, though, since that is passed a pointer.)Oh no, you're right!
Anyway, float is pretty much just used to save space if you need a large number of floating point values and don't need the extra precision or range of double. It could also speed up processing by holding more in the CPU cache. Otherwise you should probably just use double. If you need to be able to represent a 64-bit integer without loss of precision in a floating point value, then you need to use a long double since a regular double only represents 53 bits of precision.
Ordinary language is totally unsuited for expressing what physics really asserts.
Only mathematics can say as little as the physicist means to say. - Bertrand Russell
Since people here are very touchy about ISO 9899 standard, the format of float, double and long double aren't defined there. 6.2.5 § 10 only says that float is a subset of double, and double is a subset of long double. By "subset" this means that all of three types could have the same representation (but it's not usual!).
Later, in 5.2.4.2.2 some details about float.h are discussed and § 8 says that the values of symbols FLT_RADIX, FLT_MANT_DIG etc are implementation defined.
Usually, C follows ISO/IEC 60559 (IEEE 754:2008 equivalent standard) where float have 24 bits of precision, double, 53 and long double, 64, with 8, 11 and 15 bits of expoent for the scale factor, respectively.
SysV ABI allows for __float128 type as well (optional, available on GCC/CLANG, with support using quadmath library), with 113 bits of precision and 15 bits of expoent (scale factor). This doesn't have hardware support, so it is software implemented.