CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

Question: void main()
{
float a= 0.7;
if (a < 0.7)
printf("c");
else
printf("c++");
}
Output of the above program is c. Why? Whereas the same program with 0.8 instead of 0.7 gives c++ as the output? Why explain?

Answer: If you give value greater than .7 then it will go 2 the else part because .8 is not less than .7



Category C Interview Questions & Answers - Exam Mode / Learning Mode
Rating (0.8) By 733 users
Added on 11/17/2012
Views 8528
Rate it!
Question: void main()
{
float a= 0.7;
if (a < 0.7)
printf("c");
else
printf("c++");
}
Output of the above program is c. Why? Whereas the same program with 0.8 instead of 0.7 gives c++ as the output? Why explain?

Answer:

If you give value greater than .7 then it will go 2 the else part because .8 is not less than .7 Source: CoolInterview.com

Answered by: neha | Date: 12/25/2007 | Contact neha Contact neha

this is because of rounding the value of variable a.
i.e.
a=0.7 is rounded to
=0.699999988
and the constant 0.7 is as
=0.69999999999
so a<0.7 ii true so it print "c"
but in case of 0.8
a=0.800000011 and
constant 0.8 is 0.8000000000000000 Source: CoolInterview.com

Answered by: Mdhav Reddy. N | Date: 12/28/2007 | Contact Mdhav Reddy. N Contact Mdhav Reddy. N

because for floating point numbers 0.7 does not really means 0.7 but it is 0.699999999 so it happens Source: CoolInterview.com

Answered by: Manish Jain | Date: 12/28/2007 | Contact Manish Jain Contact Manish Jain

Here 0.7 is represented using considering it as double.Hence is the result.

To have the Desired result say..

if(a<0.7f) //Note : 'f'
{
...
}
else
{
..
} Source: CoolInterview.com

Answered by: vinay H.M | Date: 1/3/2008 | Contact vinay H.M Contact vinay H.M

if(a<0.7) condition is evaluated as false(0) by the compiler. so the output of program is 'c'.

whereas for 0.8 instead of 0.7, the condition is evaluated as true(1) so the output of the program is 'c++'. Source: CoolInterview.com

Answered by: Kinjal Patel | Date: 1/3/2008 | Contact Kinjal Patel Contact Kinjal Patel

the value 0.7 will be stored in variable a as 6.99999 which is less than 0.7 Source: CoolInterview.com

Answered by: SHINOY M S | Date: 1/3/2008 | Contact SHINOY M S Contact SHINOY M S

In c any real number is treated as double.
So, when you compare 'a' with 0.7, complier is comparing float number with a double which results into printing - C.
To tell the compiler that you want to compare 'a' with float 0.7 you can change the program as follows:
#include<stdio.h>
void main()
{
float a = 0.7;
if(a <0.7f)
printf("C");
else
printf("C++");
}
Source: CoolInterview.com

Answered by: rashmi bhatt | Date: 1/4/2008 | Contact rashmi bhatt Contact rashmi bhatt

Answer is C
since a is float.and 0.7 is double
when it compare a(float)<0.7(double)
means double is greater than float
so it is true . Source: CoolInterview.com

Answered by: Bipin | Date: 1/4/2008 | Contact Bipin Contact Bipin

A float number is always less than a double variable.Because 0.7 is by default a double variable. Source: CoolInterview.com

Answered by: Umakanta Mahanta | Date: 1/6/2008 | Contact Umakanta Mahanta Contact Umakanta Mahanta

This depends on the storage of a float value. The decimal values will be stored in binary form (....,2^3,2^2,2^1,2^0,.,2^-1,2^-2,2^-3,...)

So, when 0.7 is stored in multiples of 2 as .101b(which is not 0.7 but 0.699999988).Hence its value will be less than 0.7.

When the value of 0.8 is stored, it is stored as .111b(which is greater than 0.8 nearly 0.800000012).

Hence the same program with 0.8 gives C++ whereas 0.7 gives c as the output.

If someone thinks, if odd numbers will be print C, then it is wrong. Because for 0.5 there is an exact match in binary i.e .1b.So it will print C++.

NOTE: here 'b' refers to binary


Source: CoolInterview.com

Answered by: Syed Baseer Ahmed | Date: 1/7/2008 | Contact Syed Baseer Ahmed Contact Syed Baseer Ahmed

All floating values like 0.1 or 0.7 r treated as double values internally.here a is float value and 0.7 is double value therefore double has more precisions than float.In comparison operation it checks each precision values. as all precision values are not equals it will go in else part and print c++. Source: CoolInterview.com

Answered by: shilpa | Date: 1/10/2008 | Contact shilpa Contact shilpa

floating point number can't be compared directly. So in order to compare the numbers we need to find the absolute value of the floating point (i.e precision) and then compare the number. Source: CoolInterview.com

Answered by: chini | Date: 1/12/2008 | Contact chini Contact chini

As the value of a is given as 0.7 the condition in if block is a<0.7 as it makes sense that 0.7=0.7 if block satisfies the condition.so it prints c.
Source: CoolInterview.com

Answered by: naresh | Date: 1/13/2008 | Contact naresh Contact naresh

By default, the compiler takes value of 0.7 in the line " if (a < 0.7)" as double and that of a is float. Hence the difference. But for 0.8, the value of double and float are the same. Source: CoolInterview.com

Answered by: gavid | Date: 1/19/2008 | Contact gavid Contact gavid

This would give an error. As there should be a bracket after if and after else block ends. Source: CoolInterview.com

Answered by: Sanchay | Date: 1/25/2008 | Contact Sanchay Contact Sanchay

The float value always takes some value like 0.6999 which is less than 7. Hence c gets printed.
To get an exact 7, use double. Source: CoolInterview.com

Answered by: saurabh maur | Date: 1/27/2008 | Contact saurabh maur Contact saurabh maur

Output will be c++ because compiler takes 0.7 as the double.Thats why answer will be c++. Source: CoolInterview.com

Answered by: sudhanshu | Date: 2/1/2008 | Contact sudhanshu Contact sudhanshu

In C, floating point constant is stored as a long double,because of precision considerations.That's why,in 'a',instead of 0.7,0.666698 gets stored.Hence the answer....while comparing,do:
while(a<(float)0.7) Source: CoolInterview.com

Answered by: Sagar Natekar | Date: 2/10/2008 | Contact Sagar Natekar Contact Sagar Natekar

This one i.e "a" will be treated as .699999 by the compiler not .7
Similarly .8 will be treated as .799999 by the compiler. Source: CoolInterview.com

Answered by: RANJAN SARKAR | Date: 2/15/2008 | Contact RANJAN SARKAR Contact RANJAN SARKAR

Whenever we use the floating point number then that floating point number is stored in to memory into the 32 bit (IEEE)(754) format that means in (sign, exponent, mantissa ).
if you will convert the 0.7 then the mantissa value will be .69999.so that it will print output "C".
Source: CoolInterview.com

Answered by: Ravi | Date: 3/18/2008 | Contact Ravi Contact Ravi

we have problems with decimal to binary conversions for .7. it cannot b expressed correctly in finite binary. it is always rounded of to .69999999 for that .
howver .8 is exact in binary too Source: CoolInterview.com

Answered by: Sarathi | Date: 5/7/2008 | Contact Sarathi Contact Sarathi

There is a limitation of floating no in c.Variable cant store exact value of float.It stores some of less value.
In this case it store the less than .7 value.so if condition becomes true and answer will be c. Source: CoolInterview.com

Answered by: neha vora | Date: 5/15/2008 | Contact neha vora Contact neha vora

if u want that your code respond with the correct value then
try this

if(a<.7f)
{
cout<<"true";
}
else
{
cout<<"false";

}

this is bcoz in condition the value of the .7 is considered as double value you need to b it in float. Source: CoolInterview.com

Answered by: Anand dave | Date: 7/13/2009 | Contact Anand dave Contact Anand dave

float is not used in if statment Source: CoolInterview.com

Answered by: VINOD KALKUMBE | Date: 11/10/2009 | Contact VINOD KALKUMBE Contact VINOD KALKUMBE

because whatever we enter in the quotation mark in the printf statement will appear exactly same as it is written in the program... Source: CoolInterview.com

Answered by: Krittika Chatterjee | Date: 5/19/2010 | Contact Krittika Chatterjee Contact Krittika Chatterjee

reason is , when 0.7 stored in a , due to precision considerations it is stored as something less than 0.7. naturally , when value stored in a is compared with 0.7 the condition evauates to true and 'c'gets printed
this problem has two solutions
a)declare a as a long double as -
long double a;
b)typecast 0.7 to a float while comparing -
if(a<(float)0.7)
Source: CoolInterview.com

Answered by: Smriti | Date: 6/22/2010 | Contact Smriti Contact Smriti

in this pgm a is treated as 0.699999
so the result is c. Source: CoolInterview.com

Answered by: santosh | Date: 8/20/2010 | Contact santosh Contact santosh

floating point constant is nothing but double so double is always greater than float hence condition is true the output is "c" Source: CoolInterview.com

Answered by: ramesh b penchala | Date: 8/23/2010 | Contact ramesh b penchala Contact ramesh b penchala

becouse it stored as 0.69999. but if you write it as (float)0.7 the it will print c++ Source: CoolInterview.com

Answered by: karam | Date: 8/23/2010 | Contact karam Contact karam

if u want that your code respond with the correct value then
try this

if(a<.7f)
{
cout<<"true";
}
else
{
cout<<"false";

}

this is bcoz if condition Source: CoolInterview.com

Answered by: ricky | Date: 9/10/2010 | Contact ricky Contact ricky


If you have the better answer, then send it to us. We will display your answer after the approval

Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.

Post your answer here

Inform me about updated answers to this question.
Related Questions
View Answer
What is difference between the test effort and the test procedure?
View Answer
How to write a program for pascal triangle?
View Answer
Write a program to remove comment lines and blank lines from an error free c program.
View Answer
Where is the prototype of the main function i.e how the execution startd from main function and how to find out the size of a function.
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All C Interview Questions & Answers - Exam Mode / Learning Mode




India News Network
Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
Cache = 0.027344 Seconds