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. help to place a menu inside a menu
View Tutorial          By: sandy at 2009-07-29 05:57:28

2. Including the required mail properties should solv
View Tutorial          By: Rajesh Vaddi at 2011-12-13 23:55:15

3. Whenever i run this program, it terminates immedia
View Tutorial          By: Gopi at 2015-08-19 06:55:41

4. i see that lots of people get this exception : jav
View Tutorial          By: oded at 2008-09-28 03:33:33

5. Following question to this topic:
- when i

View Tutorial          By: Alistair at 2012-01-14 10:59:52

6. Excellent information reg the scopes of jsp , plea
View Tutorial          By: venkateshwarreddy suravaram at 2012-01-09 08:34:23

7. i am looking for the code which wiLL HAVE AFFECT I
View Tutorial          By: pooja at 2013-06-10 13:48:32

8. Your Work is Good, But i want to ask if there is a
View Tutorial          By: Good Muyis at 2011-01-08 09:47:14

9. The best explanation....Thanq :)
View Tutorial          By: Gautham at 2012-01-29 05:05:48

10. your web site is very useful for students
View Tutorial          By: aathithi at 2008-06-22 22:41:07