Nayaz
asked

Expert
Udayan Shakya answered
Hi Nayaz,
Good question. You’re right that the code will still run without the f, but here’s what’s really happening:
When you write:
float n = 1.2;
the value 1.2 is treated as a double by default. Then it's converted to a float, which can lead to a small precision loss — because double uses 8 bytes, while float uses only 4.
If you write:
float n = 1.2f;
you’re telling the compiler directly: “this is a float value,” and it avoids any unnecessary type conversion or warning.
So while the f isn’t strictly required, it’s considered good practice when assigning float literals.
If you have more questions, I am here to help.
C++
This question was asked as part of the Learn C++ Basics course.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer