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. The best explanation....Thanq :)
View Tutorial          By: Gautham at 2012-01-29 05:05:48

2. Good Explanation for the collections. The output f
View Tutorial          By: kalaivanan at 2012-04-11 06:51:49

3. How to fix this Error.....

org.apac

View Tutorial          By: james prabhakaran at 2014-12-02 13:08:30

4. Daniel Jonsson, you should be thankful to this art
View Tutorial          By: dole at 2011-11-12 06:44:42

5. plz i want afull code to send sms to mobile includ
View Tutorial          By: alosh at 2010-10-17 08:39:25

6. very helpful to me.thanks.very very useful to me.
View Tutorial          By: viji at 2007-12-21 03:17:14

7. Hi, this blog answers my questions but what is the
View Tutorial          By: Edu at 2009-09-20 00:19:27

8. sir i want how to write record into file and readi
View Tutorial          By: himasekhar at 2013-05-25 10:38:31

9. fantastic tutorial and very well explained, I've o
View Tutorial          By: Francis at 2011-08-14 19:57:18

10. regarding 'TextMessage' and 'MessageConnection' er
View Tutorial          By: gaurav at 2012-04-24 11:57:17