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
- React Native
- 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
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)