What's the difference between LONG float and double in C++?
There is no such a type as long float
within my knowledge.
This post gives you information about why people use lf
to print double
with printf
if this is the cause of your confusion.
By courtesy of @Jerry Coffin:
"%f" is the (or at least "a") correct format for a double. There is no format for a float, because if you attempt to pass a float to printf, it'll be promoted to double before printf receives it. "%lf" is also acceptable under the current standard -- the l is specified as having no effect if followed by the f conversion specifier (among others).
So the reason is that when people do:
printf("The number is %lf", number);
It is equivalent to do:
printf("The number is %f", number); //l has no effect when printing double
printf
specifier names don't have anything in common with names of types.
They are just named that way so they are short and easy to remember.
float -> double -> long double
%f -> %lf -> %Lf
(also, they couldn't name printf
double specifier as %d
because that name is already reserved for decimal representation of int
(compared to octal %o
))
@taocp's answer explains why you can use both %f
and %lf
with printf
, but note you can't do it with scanf
The long float
is a K&R C first edition type that existed. It is synonymous with double
.
After the first standard C89/C90, long float
is removed. It is not deprecated. C89/C90 is also K&R C second edition. Then there is the multilingual amendment known as C94/C95 that adds wchar_t
, as well as features such as <iso646.h>
.
Many features of K&R C first edition are deprecated but not removed until the second standard C99. Automatic return type to int is removed, and default parameter type to int and double is removed from C99. The C99 standard requires function prototype, not function declaration i.e. int function_declaration();
vs int function_prototype(void);
. It also removed the K&R C style prototype.
// implicit int type
main(argc, argv)
char ** argv;
// explicit int type
int main(argc, argv)
int argc;
char ** argv;
C++ started long before C was standardized. Templates were standardized in 1983, making it harder to compile to C code. It was not standardized until 1998. Old compilers may have deprecated old features that are removed with more contemporary compilers. The %lf
is a legacy of long float
that gets carried forward for C's standard library.