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



Subscribe to Tutorials

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. your second last example in above code (Character
View Tutorial          By: rupam shaw at 2013-04-24 08:36:05

2. Hi Ramalak, I am suffering with the same error as
View Tutorial          By: Divya at 2008-11-06 22:46:12

3. why return&else statment repeating?
View Tutorial          By: sudhar at 2012-07-12 14:50:45

4. sir the program is
error: error reading C:

View Tutorial          By: Yashovardhan at 2008-05-05 21:42:27

5. Nice one really helpful thanks
View Tutorial          By: Shashikant at 2012-01-06 12:00:45

6. Thanks , really appreciate
View Tutorial          By: Marlen at 2010-11-23 10:50:29

7. this code runs permanently!!! created by diko(Turk
View Tutorial          By: ali at 2013-08-12 07:56:01

8. javax.microedition.media.MediaException: Malformed
View Tutorial          By: adi at 2012-04-01 11:51:17

9. In your example http://twitter.com/statuses/updat
View Tutorial          By: thomas wilkinson at 2012-11-02 01:03:45

10. Thanks, this is exactly i am looking for. I need t
View Tutorial          By: Ankita at 2009-11-15 19:41:16