C Programming Tutorial 40 - How to Use the Type Cast Operator

submitted by mkenny400 on 03/19/18 1

Now, let's give an example of explicit conversion: int slices = 17; double halfThePizza = slices / 2; In this situation, we are splitting the pieces of pizza with our friend. We are trying to figure out how many pieces each person should get. Both operands are integers, so the result will be an integer. There are two ways we can fix this. First, we can add .0 to the 2. This works, but it will not always work. For example, we could have this: int slices = 17; int people = 2; double slicesPerPerson = slices / people; Print this out and see that the result of the double is actually 8, not 8.5. Let's assume that the second operand is given to us as an integer and we cannot change it for some reason. The only solution is to use an explicit cast: double slicesPerPerson = (double) slices / people; This explicitly casts the value of slices to be a double. Now, when you output the data, it should be 8.5. The important thing to know here is that the cast does not affect the original value of the variable. The casting is actually just another operator. Think of it as a unary operator that does not change the value of the variable. The value of the variable is first given, and then that value is changed to a double, but the original variable stays the same. Once you define a variable as a certain type, you're stuck with it. It's important to think of the casting as a unary operator. That means that it only works on one operand. Which operand? is it this: double slicesPerPerson = (double) (slices / people); or is it: double slicesPerPerson (double) (slices) / peoples; It's actually the second one. It only affects the thing to the direct right. You can cast an entire expression using parenthesis, so the first one is completely valid. The only problem is that the result will still be 8. That's because we divide two ints by one another and get 8, and then we cast 8 to a double, which stays 8. Because of this potential area of confusion, I recommend you try to avoid casting a large expression that is in parenthesis unless you know exactly how it is going to work. TRIVIA TIME!!! What will this output? double c = 25 / 2 * 2; double d = 25 / 2 * 2.0; printf("c: %f\n", c); printf("d: %f\n", d); This program illustrates that even if one of the operands is floating point, the entire expression does not use floating point arithmetic. In the case with the variable d, the 25/2 evaluates to 14, then * 2 evaluates to 24.0. Both printf()'s output the same thing. Remember two that the decimal is always truncated, never rounded. So even if some division ended with .99999, it would just delete the .99999 and not round up. HELP ME! www.patreon.com/calebcurry Subscribe to my newsletter: eepurl.com/-8qtH Donate!: bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: CalebCurry.com Facebook: www.facebook.com/CalebTheVideoMaker Google+: plus.google.com/+CalebTheVideoMaker2 Twitter: twitter.com/calebCurry Amazing Web Hosting - www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Leave a comment

Be the first to comment

Collections with this video
Email
Message
×
Embed video on a website or blog
Width
px
Height
px
×
Join Huzzaz
Start collecting all your favorite videos
×
Log in
Join Huzzaz

facebook login
×
Retrieve username and password
Name
Enter your email address to retrieve your username and password
(Check your spam folder if you don't find it in your inbox)

×