Friday, 10 September 2021

Can we use floating variables in if statements in C?

Yes, we can use floating variable in if statement but you have to be very careful while initialising the condition C Language.

Comparisons between floating-point values (including comparisons with zero) are easily sent “off” by truncation/rounding errors.

Yes. You can use. But a direct comparison will never result in a true condition.

Remember that floating point values are hardly precise. So what you can do is along with the float value you are comparing, declare another value that serves as an upper and lower bound to your value,

 


For example:-

Float a=6.7, b=5.8;

if(a==b)

{

            printf("same");

}

Float a=1.0;

If (a==1) // this will be wrong in this case as condition will never be true

To do this correctly you have to write like this

If (a==1.0){

}

To learn more about Data Type, If Condition, C Programming Language, Computer Course.

TCCI coaching institute teach all programming language to all school boards, all university, all branch students and any non-IT person.

Join us @ TCCI.

Call us @ 9825618292

Visit us @ http://tccicomputercoaching.com/

No comments:

Post a Comment