Reputation: 12953
This article says:
If I write a line of code like this inside a function:
return 1.4
, It is obvious to both me and the compiler that the function is returning a double.
It is not obvious to me: the return type could be a float, a double, or a long double. How does the compiler choose between the 3 types?
Upvotes: 4
Views: 172
Reputation: 20264
No, 1.4
is a double
. float
is written as 1.4f
75 // int
75u // unsigned int
75l // long
75ul // unsigned long
75lu // unsigned long
3.14159L // long double
6.02e23f // float
Upvotes: 3
Reputation: 1612
1.4 is double here, any simple decimal point number is double.
float will be 1.4f
long double will be 1.4L
1.4 // double
1.4f // float
1.4L // long double
Upvotes: 1
Reputation: 50043
I guess you are asking about auto
return type deduction, otherwise the return type is whatever you declare it to be.
The answer is straight forward: The literal 1.4
has type double
, not float
or long double
, so double
will be deduced. Easy as that.
Upvotes: 2
Reputation: 129314
Because 1.4 (or any other floating point number) is double
. To make it float
you need to write 1.4f
, and long double
is 1.4L
.
Upvotes: 1
Reputation: 15229
1.4
is a double
literal as opposed to the float
literal 1.4f
and the long double
literal 1.4l
.
Therefore, the compiler can deduce the type unambiguously.
Upvotes: 3