JORDAN JOZWIAK: Type casting, in the simplest sense, is a way to alter a computer's interpretation of some data by implicitly or explicitly changing its data type. Such as changing an int to a float, or vice versa. To understand type casting, we need to start with the basics-- data types themselves. In computer languages like C, all variables have some sort of data type that determines how the computer, and likewise the user, interprets that variable. Numerical data types such as an int, long long, float and double, all have their own unique characteristics and are used to specify values of varying ranges and precision. Type casting allows us to take a floating point number like 3.14 and get the part before the decimal, 3 in this case, by casting it to an int. Let's take an example from the English language for a brief review of types, and to see how type casting can change the way we interpret a piece of data. For data, let's take the symbols here. I just refer to these carefully configured lines as symbols, but as somebody who knows the English language, you immediately recognize that they are, in fact, letters. You implicitly understood the data type. Looking at this string of letters we can see two different words, each with its own meaning. There's the noun, wind, as in the wind blows outside. And there's the verb, wind, as in I need to wind my analogue watch. This is an interesting example, because we can see how the type that we assign to our data, whether noun or verb, changes how we use that data-- as the word wind or wind. Although a computer doesn't care about grammar and parts of English speech, the same basic principle applies. That is, we can change the interpretation of the exact same data stored in memory by simply casting it to a different type. Here are the sizes of the most common types on a 32-bit operating system. We have a char at 1 byte, int and float at 4 bytes, a long long and a double at 8 bytes. Because an int takes up 4 bytes, it will take up 32 bits when it is stored in memory as a binary series of zeros and ones. As long as our variable remains as a type int, the computer will always convert those ones and zeros from binary into the original number. However, we could theoretically cast those 32 bits into a series of Boolean types. And then the computer would no longer see a number, but instead a collection of zeros and ones. We could also try to read that data as a different numeric type, or even as a string of four characters. When dealing with numbers in casting, you must consider how the precision of your value will be affected. Keep in mind that the precision can stay the same, or you can lose precision, but you can never gain precision. Let's go through for the three most common ways that you can lose precision. Casting a float to an int will cause truncation of everything after the decimal point, so you're left with the whole number. If we take the float x which will equal 3.7, we can cast this variable x to an int by simply writing int in parentheses. Whenever we use this term right here, we'll effectively be using the value three because we've truncated everything after the decimal point. We can also convert a long long to an int, which will similarly lead to a loss of high-order bits. A long long takes up 8 bytes, or 64 bits in memory. So when we cast it to an int which only has 4 bytes, or 32 bits, we are essentially chopping off all the bits that represent the higher binary values. You could also cast a double to a float, which will give you the closest possible float to the double without necessarily rounding it. Similar to our long long to int conversion, the loss in precision is because a double contains more data. A double will allow you to store 53 significant bits, roughly 16 significant digits. Whereas a float will only allow you to store 24 significant bits, roughly seven significant digits. In these last two cases, it may be helpful to think of type casting as resizing a photo. When you go from a large size to a small size, you can't see things as clearly because you lost data in the form of pixels. Type casting can also cause trouble when we cast ints to floats. Since floats on a 32-bit machine only have 24 significant bits, they cannot accurately represent values over 2 to the power of 24, or 16777217. Now let's talk about explicit and implicit casting. Explicit casting is when we write the type in parentheses before a variable name. As an example, before we wrote int in parentheses before our float variable x. In this way, we get the int value, the truncated value of 3.7-- 3. Implicit casting is when the compiler automatically changes similar types to a super type, or performs some other sort of casting without requiring the user to write any additional code. For example, when we add 5 and 1.1, our values already have types associated with them. The 5 is an int, whereas 1.1 is a float. In order to add them, the computer casts 5 into a float, which would have been the same thing as writing 5.0 in the first place. But this way we say float 5, or 5.0, plus what was already a float, 1.1, and from there we can actually add these values and get the value 6.1. Implicit casting also allows us to assign variables of different types to each other. We can always assign a less precise type into a more precise one. For instance, if we have a double x, and an int y-- and these could have any values that we set them to-- we can say x equals y. Because the double has more precision than an int, so we won't lose any information. On the other hand, it wouldn't necessarily be correct to say y equals x, because the double might have a larger value than the integer. And so the integer might not be able to hold all the information stored in the double. Implicit casting is also used in comparison operators like greater than, less than, or the equality operator. This way we can say if 5.1 is greater than 5, and we get the result true. Because 5 is an int, but it'll be cast to a float in order to be compared to the float 5.1, we'd say 5.1 is greater than 5.0. The same is true with saying if 2.0 equals equals 2. We'd also get true, because the computer will cast the integer 2 to float and then say 2.0 equals equals 2.0, this is true. Don't forget that we can also cast between ints and chars, or ASCII values. Chars also need to be reduced to binary, which is why you can easily convert between chars and their respective ASCII values. To learn more about this, check out our video on ASCII. When you take a moment to think about how data's stored, it begins to make a lot of sense. It's just like the difference between wind and wind. The data is the same, but the type can change how we interpret it. My name is Jordan Jozwiak, this cs50.