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. Thanks sir
View Tutorial          By: sivaraj at 2013-01-23 05:43:35

2. I need some help. I am a programmer - but quite n
View Tutorial          By: Ron at 2013-06-21 00:58:27

3. all are easy to understanding codes. . . thanks
View Tutorial          By: kadhir at 2011-06-27 03:33:30

4. Thanks!
View Tutorial          By: misterTi at 2015-03-18 17:48:41

5. Very good. but why i can search data then report v
View Tutorial          By: ARMAN AHMED at 2015-04-27 13:18:04

6. can u give me the example simulation of strcat??
View Tutorial          By: farah at 2009-10-05 01:10:02

7. Your solution is really helped me. Thanks for your
View Tutorial          By: Rajakumar at 2010-05-31 06:05:53

8. Thank you so much....
I am going to have ja

View Tutorial          By: Mehrnoosh at 2013-06-09 16:50:23

9. thanks. worked.
View Tutorial          By: che at 2010-03-10 11:29:10

10. very easy and simple program..
need minor

View Tutorial          By: Metal at 2014-05-07 10:31:04