Double vs float C/C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Double vs float C/C++

Why does this code output 1 and not 2? #include <iostream> using namespace std; void f(float x){cout << 2;} void f(double x){cout << 1;} void f(int x){cout << 3;} int main() { f(1.0); return 0; }

1st Apr 2021, 8:27 PM
Edward Finkelstein
Edward Finkelstein - avatar
3 Answers
+ 3
Precision numbers default by double types.. so 1.0 is double type. So it calls f(double x){}. Use 1.0f for float type function callings...
1st Apr 2021, 8:33 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna, ah ok, I didn’t know that, thank you!
1st Apr 2021, 8:47 PM
Edward Finkelstein
Edward Finkelstein - avatar
+ 1
purpose of this q/a section, know about.. You're welcome..
2nd Apr 2021, 11:44 AM
Jayakrishna 🇮🇳