PRO
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
