[✔️]Why output of this code is ''not equal'' ? Please explain! i don't understand this.! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

[✔️]Why output of this code is ''not equal'' ? Please explain! i don't understand this.!

#include <stdio.h> int main() { float f = 0.1; if (f == 0.1) printf ("equal"); else printf ("not equal"); return 0; } Output : not equal

1st Aug 2021, 10:32 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
25 Answers
+ 12
It outputs "not equal" because your variable is a float but 0.1 is not. 0.1 is a double by default. To make it float add 'f' at the end. (e.g. 0.1f) This code will output "equal", as required: #include <stdio.h> int main() { float f = 0.1; if (f == 0.1f) printf ("equal"); else printf ("not equal"); return 0; }
1st Aug 2021, 10:38 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 4
Here you decleared one float variable f and you assigning value 0.1 remember that this is double value not float becz by default float value treat as double type but here f type is float and you assigning double value to float variable f . If you type cast lower data type to higher like Short to int ,int to float , float to double. but in case of double to float it means here u converting Higher data type to lower. Both data types have different different ranges so if u will try to assign value it will loose some precision and your data will be loose . If you avoid this kind of problems in your code in c++ you can use static_cast<data type> same thing happening in your case. If u want to define float variable then you can write like this float f=0.1f ; if didn't get answer then try this one your doubts will be clear 0.1is a double value by default when assigned to float it will loose some precision hence it will not compare equal to 0.1 printf("%.20f\n", a); printf("%.20f\n",0.1);
1st Aug 2021, 10:54 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 4
Ooooooh..😅 Great to know. And because of this reason i failed so many times in C programs.
1st Aug 2021, 11:02 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
+ 3
#include <stdio.h> int main() { float a=9.6; printf("%.20f\n", a); printf("%.20f\n",9.6); return 0; }
1st Aug 2021, 11:20 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Aleksei Radchenkov , 😮 Ohh. But in cpp by default any decimal number is double, right ? So is it also in c ?
1st Aug 2021, 10:49 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
+ 2
I don't use C too much, but I believe it's the same as C++
1st Aug 2021, 10:50 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Okay. Anyways, thanks for explaining. 😊
1st Aug 2021, 10:52 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
+ 2
#...._._...._<........> Yeah it will give same in c ,cpp
1st Aug 2021, 10:59 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
A.S. , Yeah this statement is working in C -->float f = 0.1f; Thank you so much for this help. and explaining. 😊
1st Aug 2021, 11:10 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
+ 2
1st Aug 2021, 11:20 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
#...._._...._<........> Just try this: printf("%0.12f\n%0.12f", (double) 0.1f, 0.1); Edit: It should be obvious that this should be included inside the main function.
2nd Aug 2021, 8:26 AM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Saifullah , Thank you. This code is working. 😊
3rd Aug 2021, 4:22 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
+ 2
Calvin Thomas , But still i don't understand this. 😅 And its not working on sololearn's compiler. 🙃
3rd Aug 2021, 4:24 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
+ 2
User_dead ; Calvin Thomas Sorry both of you. Your given code is right and working. 🙌 that was my mistake, i wrote my code in a wrong way.😬
3rd Aug 2021, 11:14 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
+ 2
Just Practice on it then you will be become a good programmer. I am also beginner bit I trying my best for the best.
3rd Aug 2021, 11:27 AM
Saifullah Khan
+ 1
#...._._...._<........> Try this statement your doubts will be more clear float a=9.6; printf("%.20f\n", a); printf("%.20f\n",9.6);
1st Aug 2021, 11:12 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Whenever you have to deal with floating points number use double data type becz it give more accurate value than float
1st Aug 2021, 11:14 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
A.S. , I use this statement [ printf ("%0.20f \n",9.6); ] but this line give me error.
1st Aug 2021, 11:18 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
+ 1
A.S. , Yeah ! Iam sure. Now i just this. These two line gives error.
1st Aug 2021, 11:23 AM
SARVESH ASHOK DEVRUKHAKAR
SARVESH ASHOK DEVRUKHAKAR - avatar
+ 1
#...._._...._<........> 🙃😆 Check the code which i have pasted
1st Aug 2021, 11:25 AM
A S Raghuvanshi
A S Raghuvanshi - avatar