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. Sir I want to information about "how to send
View Tutorial          By: murthy at 2010-02-17 07:15:18

2. HI Friends,

I am new to java and i

View Tutorial          By: Srini at 2015-04-05 17:00:58

3. works like a charm, though i didn't understand why
View Tutorial          By: primus at 2010-05-19 04:46:18

4. abstract class A {
abstract void callme()

View Tutorial          By: satish at 2012-04-07 10:35:38

5. which method is called if i call overriden method
View Tutorial          By: sarah at 2012-02-01 13:36:10

6. Nevermind, I was able to modify both applications
View Tutorial          By: Matthew at 2009-06-16 14:10:20

7. Hi All,

I am new to J2ME Applicatio

View Tutorial          By: golemnagesh at 2010-12-05 22:43:50

8. "THIS IS SAMPLE EXMPLE!!!!!!!!!!11111"
View Tutorial          By: david billa at 2012-08-02 06:23:43

9. I was able to modify this to work perfectly.

View Tutorial          By: _Jon at 2012-04-01 23:04:49

10. I take it English is not your 'hardcore', because
View Tutorial          By: Fred at 2013-01-17 14:49:09