| float/double/long double are floating point "real" numbers. C/C++ uses double (aka. double-precision) by default for representing real values. so you should prefer double when holding real values.
float is typically smaller than double, with lesser precision. long double is known as extended precision, which is typically greater than double.
Though you'll inevitably come across compilers where one or more of these types are the same size.
The only guarantee you have is that sizeof(float) <= sizeof(double)
and sizeof(double) <= long double
Generally, float isn't used very often, it may be useful if you're economising on space, and don't need the precision (Such as sending bits over a network stream). long double is useful if you need the extra precision where its available.
wchar_t is typically used for holding unicode values (useful when support for foreign language character sets is needed)
as for long/short - These are shorthand for long int and short int. As is the case for the real types above, you've only got one guarantee for each..
sizeof(char) == 1
sizeof(char) <= sizeof(short)
sizeof(short) <= sizeof(int)
sizeof(int) <= sizeof(long int)
in general, use int. which is the default integral type in C/C++. If you need a bit more range than int can provide, then use long, be aware that long may be the same size as int. (if that's not enough, use a "Big Number" library)
If you're economising on space, you might feel the need to use short, although in practice, its not used all that commonly. Again, being aware that short can be the same size as int.
If you're not sure what range or precision you need, start with the default ones - char for characters, int for integrals and double for real numbers. If it transpires later that one of the others are needed, then you can change it.
Although swapping char for wchar_t is a little trickier, it has seperate string and I/O facilities to plain char. generally these are prefixed with 'w' - eg, wcout, wcin, wstring.
Last edited by Bench : 09-03-2007 at 05:18 AM.
|