C Programming Tutorial 38 – Type Casting

In the previous video we mentioned how C was a strongly typed language. Imagine C as a grumpy old man who you owe a dollar bill to. Well, he wants his money now, but all you have is some change. you look at your change, grab four quarters, and find out that it adds up to exactly a dollar. But unfortunately, you owe him a dollar bill, not just 100 cents. So yeah, he beats you over the head with his cane.

That is the way you ought to look at data types in C. You could say that the guy wanted 1 dollar, but you gave him .25 + .25 + .25 + .25 = 1.00. This my friend is unacceptable.

Well, to fix this problem, you could go the bank, exchange your four quarters for an actual dollar, and then go pay the man. The bank in this situation has fixed your problem by converting your change to whole dollars. In computer science terminology, you can say that your number of the double data type was casted to a number of the integer data type.

So, type casting is the process of converting one data type to another.
 
There are two main categories of type casting, implicit and explicit type casting.
 
Implicit type casting is when the conversion happens automatically and you do not have to worry about it.
 
Explicit type casting is when you have to say specifically that you want to convert some data from one data type to another. You can think of this as “explicitly” telling the computer what to do. It’s not just assumed.
 
In general, you can think of the implicit type casting as “lossless”. A good example of this is converting an integer to a double. If you convert 1 to 1.00, nothing is lost.
 
You can think of explicit type casting as “lossy”. For example, if you convert the double value 2.54 to an integer, you truncate the number down to 2.
 
An example of implicit conversion is if you assign 1 to a double value. You gave the assignment operator an integer, but it gets stored as a double. There’s really nothing you have to do.
 
The easiest way to explicitly type cast is to use an operator where you put the type you want the value to be in parenthesis before the data. For example, we could do something like (int) 2.5. This will convert the 2.5 to 2 (an integer).
 
In the next video we will be going over some examples of implicit and explicit conversions and some things to look out for that can cause problems.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Support me! http://www.patreon.com/calebcurry

Subscribe to my newsletter: http://bit.ly/JoinCCNewsletter

Donate!: http://bit.ly/DonateCTVM2.

~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~

More content: http://CalebCurry.com
Facebook: http://www.facebook.com/CalebTheVideoMaker
Google+: https://plus.google.com/+CalebTheVideoMaker2
Twitter: http://twitter.com/calebCurry

Amazing Web Hosting – http://bit.ly/ccbluehost (The best web hosting for a cheap price!)

Comments

Lisa H says:

Thank you! Your videos are very helpful :)
Please continue to share your knowledge with the world!

LOGIO says:

Very clear and creative explanation .

Lucas says:

your hair is on fire boy

Zephan Patton says:

that there hair tho

Write a comment

*

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!

Send this to a friend

▷ Other ReviewsVehicles▷ Show Cars▷ Motorbikes▷ Scooters▷ Bicycles▷ Rims & Tires▷ Luxury BoatsFashion▷ Sunglasses▷ Luxury Watches▷ Luxury Purses▷ Jeans Wear▷ High Heels▷ Kinis Swimwear▷ Perfumes▷ Jewellery▷ Cosmetics▷ Shaving Helpers▷ Fashion HatsFooding▷ Chef Club▷ Fooding Helpers▷ Coktails & LiquorsSports▷ Sport Shoes▷ Fitness & Detox▷ Golf Gear▷ Racquets▷ Hiking & Trek Gear▷ Diving Equipment▷ Ski Gear▷ Snowboards▷ Surf Boards▷ Rollers & SkatesEntertainment▷ DIY Guides▷ Zik Instruments▷ Published Books▷ Music Albums▷ Cine Movies▷ Trading Helpers▷ Make Money▷ Fishing Equipment▷ Paintball Supplies▷ Trading Card Games▷ Telescopes▷ Knives▷ VapesHigh Tech▷ Flat Screens▷ Tech Devices▷ Camera Lenses▷ Audio HiFi▷ Printers▷ USB Devices▷ PC Hardware▷ Network Gear▷ Cloud Servers▷ Software Helpers▷ Programmer Helpers▷ Mobile Apps▷ Hearing AidsHome▷ Home Furniture▷ Home Appliances▷ Tools Workshop▷ Beddings▷ Floor Layings▷ Barbecues▷ Aquarium Gear▷ Safe Boxes▷ Office Supplies▷ Security Locks▷ Cleaning ProductsKids▷ Baby Strollers▷ Child Car Seats▷ Remote ControlledTravel▷ Luggages & Bags▷ Airlines Seats▷ Hotel Rooms▷ Fun Trips▷ Cruise Ships▷ Mexico Tours