The previous video was foundational to this video, so go watch that! Essentially, casting is when something of a certain data type is converted to a different data type.
This video is going to be showing examples of both implicit casting and explicit casting.
When you study implicit conversion in C, you will likely come across the term promotion.
This article talks about implicit conversion, and the section on promotion talks about what data types can be converted to other data types without loss of information.
A common example of this is when we pass floats to functions.
float x = 50.0f; // You can put the f here if you want to be specific that this is a float, but either way should work just the same).
The printf function actually takes the x as a double. This means that 50.0f is promoted to 50.0. If we are talking about constant values, a float value always has an f. If you leave the f off, it is assumed to be of type double. You can use the %f conversion character for both floats and for doubles because they are actually always being printed as doubles… Even if you pass in a float.
Now, it’s important to understand that the actual value or data type does not change. The variable x is still of type float.
Another common type of implicit conversion is when we are doing assignment. If we have something such as this:
double age = 60;
The integer 60 is implicitly casted to a double. That’s because an integer can fit inside of a double without losing data.
Sometimes, it’s not that easy and we risk losing data. That’s what we are going to be talking about in the next video.
HELP ME! http://www.patreon.com/calebcurry
Subscribe to my newsletter: http://bit.ly/JoinCCNewsletter
More content: http://CalebCurry.com
Amazing Web Hosting – http://bit.ly/ccbluehost (The best web hosting for a cheap price!)
source by Caleb Curry
c programming tutorial