473,382 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Why (cast) when we don't have to?

I was just reading in K&R page 44-45 that "conversions take place across assignments; the value of the right side is converted to the type on the left, which is the type of the result......If x is float and i is int, then x = i and i = x both cause conversions; float to int causes truncation"

so if I can type:

Expand|Select|Wrap|Line Numbers
  1. int i;
  2. float f;
  3.  
  4. i = f; // and f = i;
  5.  
  6.  
without explicitly casting (float) and (int), then

what's the purpose of casting???
Jun 21 '11 #1
2 1459
weaknessforcats
9,208 Expert Mod 8TB
In your example an explicit cast on your part is unnecessary. The compiler knows both types.

But many C functions have arguments that are addresses of an unknown type. These are the void* arguments. These C functions may also have a second argument (usually an int) that the function can use to determine the type of the void*. Maybe 1 means int* and 2 means Date" where Date is one of your structs. The function can now typecast the void* into the correct type.

In short, you use an explicit cast when only you know what the real type is.
Jun 21 '11 #2
Banfa
9,065 Expert Mod 8TB
You might also using casting while doing arithmetic operations in order to ensure you get the correct result, for example

Expand|Select|Wrap|Line Numbers
  1. int a = 4;
  2. int b = 5;
  3. double c;
  4.  
  5. c = a / b; /* c has the value 0.0 because the calculation is done 
  6.               in integer arithmetic and then assigned to c */
  7.  
  8. c = (double)a / b; /* c has the value 0.8 because the calculation is done as 
  9.                       floating point arithmetic because a has been cast to a 
  10.                       double and then assign to c*/
  11.  
There can be similar issues resolved with casts when using integers of different bit sizes for the calculation and result.
Jun 22 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
6
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to an abstract base class OptionBase. OptionBase is not templated but I am deriving a template class called Option from OptionBase. The class Option has...
1
by: Derrick | last post by:
Hopefully someone can explain to me why this doesn't work: - I have an interface ("IFoo"), the source of which lives in "Assembly A" - A class ("Foo") which implements this interface lives in...
8
by: Mike Nolan | last post by:
As far as I can tell, Postgres has no equivalent to greatest and least functions in Oracle. Yes, you can do the same thing with a case statement, but at the expense of writing MUCH longer SQL...
9
by: Nicolas Blais | last post by:
Hi, I have this following class which I use as a timer: #include <sys/time.h> using namespace std; class chrono { public: chrono() {};
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
4
by: dmac | last post by:
Below is some really simple code - its just a trivial class used to populate a combo box from which I want to pull out one of the properties of the selected object. I am just curious to know why -...
7
by: chance | last post by:
Getting and error on this first line of code: //inspector phone theField = reader.GetString(20); builder.MoveToBookmark("inspector_phone"); builder.Write(theField); the first line is...
41
by: z | last post by:
I use Visual C 2005 to develop my programs. One in particular is crashing in very specific and hard to replicate situations, made worse by the fact it only crashes when run -outside- the dev - as...
3
by: Uta P | last post by:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.