Output of c program

float comparision in c. whats d output of below program


main()

{

     float a = 0.7;

     if(a==0.7)

         printf("a");

     else

         printf("b");

}

 

if its b why so. how is the float comparision actually done

Would you like to answer or comment?

Sign up for a free account, or sign in (if you're already a member).
  • 195 views
Share Send to a friend Watch Report
 
 

Posted Answers

Order by
 
4 thumbs up

The default floating point type is double. Your comparison compares a float variable to a double constant, hence the inequality.

You can either add an 'f' after the constant (if a==0.7f) to make it a float or define a as a double (double a = 0.7), but the best way would be to use a custom comparison function that returns true if the delta between the two numbers is very small (the Knuth way). Read more here.


Posted 3 months ago ( permalink )
In reply to praveen's question
Rated as
#1 out of 3
0
0

Helpful?

line
line
line



 

12


Posted 2 months ago ( permalink )
In reply to praveen's question
Rated as
#2 out of 3
0
0

Helpful?

line
line
line



 

12


Posted 2 months ago ( permalink )
In reply to praveen's question
Rated as
#3 out of 3
0
0

Helpful?

line
line
line



Sign in to participate

Got an answer for praveen? Would you like to comment on the posted answers, or vote for the one which you think is the best?

Sign up for a free account, or sign in (if you're already a member).

Explore Related Questions

Other people asked questions on similar topics, check out the answers they received:


Programming Language

what is the best programming language to learn when starting out .
Submitted by negativebase 2 years ago
  • viewed 593 times

Last answer posted 2 years ago by rutger


Language change

Windows live messenger appears in Spanish on my computer, how do I change it (the language) to English?
Submitted by Fen 7 months ago
  • viewed 242 times

Last answer posted 7 months ago by Femalesoldier


What does "Roque et Rolle" translate to in ...

What does "Roque et Rolle" translate to in English?
Submitted by Samuel Singer 2 months ago
  • viewed 166 times

Last answer posted 9 days ago by Marinati



» More...

Explore Related Posts in Forums

The Blue Programming Language 1.4.2 - A high-level programming language...

The Blue Programming Language 1.4 - A high-level programming language.

The Blue Programming Language 1.2 - A high-level programming language.

The Blue Programming Language 1.3 - A high-level programming language.

What programming language is Google (or all SE's) written in?

Looking for programming examples in any programming language

Blocks Language 0.1 - A block-oriented graphical programming language.

Best programming language for today and tomorrow?

Does Programming Language Matter?

Powered by
Feed - Subscribe to changes to this Q&A Blog
Copyright © 2006-2008, Yedda Inc. and respective copyright owners · CC License