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
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
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
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program 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
Archived Comments
1. sir, i am completed MCA 2011 and i want to learn P
View Tutorial By: vipin kumar at 2011-09-13 13:00:05
2. nativeFinalize: Error closing Handle 6
and
View Tutorial By: yash at 2008-05-16 17:43:42
3. hi
Can you anyone please post me the entir
View Tutorial By: yasmeen at 2009-05-25 02:42:44
4. Is it possible to create a Reader class without lo
View Tutorial By: bs at 2010-10-11 05:42:34
5. I just copied this example to my app. It's not wor
View Tutorial By: jhansi at 2012-02-22 10:26:17
6. Si AIZA DIN< DI MARUNONG MAGTURO
View Tutorial By: Agnes at 2010-06-25 22:34:29
7. not good example to learn the overriding methods
View Tutorial By: george at 2010-04-25 12:08:20
8. it provides manna in the studies of java to me
View Tutorial By: aravinden at 2011-12-27 08:18:18
9. good site for buggners
View Tutorial By: sivannaadhbaazi karampudi at 2011-12-01 15:37:55
10. Hi, I second that Raghav, kindly can u email me a
View Tutorial By: SAM at 2011-03-06 05:03:09