Comment on Tutorial - Using cout in C++ By Baski



Comment Added by : Nrom

Comment Added at : 2012-06-26 20:16:52

Comment on Tutorial : Using cout in C++ By Baski
Line 11 says:
cout << "Here's a fraction:\t\t" << (float) 5/8 << endl;

You explained:
On line 11, the value 5/8 is inserted into cout. The term (float) tells cout that you want this value evaluated as a decimal equivalent, and so a fraction is printed.

But I think (float) tells the compiler to cast the integer '5' as a float before performing the evaluation of 5/8: 5.0/8 The 8 remains an int, but it gets promoted to float during the evaluation, so the result is a float. cout then deals with the result, which is a float.

Wrapping 5/8 in parens causes the fraction to be evaluated and that result cast to a float. 5/8 is zero. The zero is cast to a float and printed.

cout << "Here's a fraction:\t\t" << (float) (5/8) << endl;


View Tutorial



Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. Puedes enviarme ese codigo, ando embolatado
View Tutorial          By: Wilson at 2011-10-26 22:45:38

2. i want very simple & easy notes on php which u
View Tutorial          By: deepa at 2011-07-12 09:04:30

3. hi,this is nishad from india.....
i have a

View Tutorial          By: NISHAD at 2008-09-06 09:23:15

4. good one !
View Tutorial          By: qasim at 2008-05-18 03:16:55

5. A phone is considered a good phone if all of the f
View Tutorial          By: harish at 2012-12-24 06:59:04

6. The code gives the following exception :-
<

View Tutorial          By: Lourdu Felix at 2009-01-26 03:37:53

7. Thanks for your nice article.I learned a lot
View Tutorial          By: Mahyar at 2012-08-12 12:09:36

8. if i want to write my gmail address how can i prov
View Tutorial          By: asmcn at 2011-12-27 19:40:49

9. thnks a lot...
View Tutorial          By: iti at 2015-06-03 07:05:24

10. Its nice and really apriciate rakesh sir i try t
View Tutorial          By: prince at 2013-04-18 09:46:34