This shows you the differences between two versions of the page.
cs:c_language:casting [2019/02/26 14:35] |
cs:c_language:casting [2020/11/26 23:18] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Casting ====== | ||
+ | **Concepts: | ||
+ | Casting of variables and mathematical expressions | ||
+ | |||
+ | **Text:**\\ | ||
+ | Some examples regarding the mathematical operations between variable (or constant) of different types, and about the casting operator, have been proposed in order to explain such arguments. | ||
+ | |||
+ | Examples have been explained with comments in the code. | ||
+ | |||
+ | **Theory: Regarding casting and mathematical expressions**\\ | ||
+ | The C programming language makes it possible to have operands with different type in the same mathematical expression. | ||
+ | |||
+ | Before actually perform the computation, | ||
+ | |||
+ | The ranks of C types, in increasing rank order are the following: | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | |||
+ | As an example, the following operation: | ||
+ | <code C> | ||
+ | 3.5+4; | ||
+ | </ | ||
+ | is an operation between a '' | ||
+ | |||
+ | The same concept holds also in the case variables are involved in a mathematical expression. For example, the following code: | ||
+ | <code C> | ||
+ | int a; | ||
+ | float result; | ||
+ | a=4; | ||
+ | result = (3.6+a)*2; | ||
+ | </ | ||
+ | stores in the variable '' | ||
+ | |||
+ | The type of a variable involved in a mathematical operation can be explicitly modified with the casting operator. The syntax of the casting operator is a type between brackets and before the variable that must be converted. | ||
+ | |||
+ | For example: | ||
+ | <code C> | ||
+ | float result; | ||
+ | int x=2, y=3; | ||
+ | result = ((float)x/ | ||
+ | </ | ||
+ | the division between '' | ||
+ | |||
+ | The same operation but with the casting operator outside the brackets: | ||
+ | <code C> | ||
+ | result = (float)(x/ | ||
+ | </ | ||
+ | stores in the variable '' | ||
+ | |||
+ | |||
+ | |||
+ | **Solution: | ||
+ | |||
+ | <file C casting.c> | ||
+ | /* Some examples about mathematical expressions and casting */ | ||
+ | |||
+ | #include < | ||
+ | |||
+ | |||
+ | int main() { | ||
+ | | ||
+ | int x = 2; | ||
+ | int y = 3; | ||
+ | float z = 3; | ||
+ | |||
+ | float result; | ||
+ | |||
+ | /* Division between integer numbers, the result is the integer | ||
+ | | ||
+ | | ||
+ | result = 2/3; | ||
+ | printf(" | ||
+ | |||
+ | /* Division between an integer and a float number, the integer | ||
+ | | ||
+ | is perform between two floats. The result is 0.666667. */ | ||
+ | result = 2.0/3; | ||
+ | printf(" | ||
+ | |||
+ | /* The same as the last operation. The number 3. is considered | ||
+ | a float number */ | ||
+ | result = 2/3.; | ||
+ | printf(" | ||
+ | |||
+ | /* Division between integer variable, the result is the integer | ||
+ | | ||
+ | | ||
+ | result = x/y; | ||
+ | printf(" | ||
+ | |||
+ | /* The casting operator (float) converts the content of the variable x | ||
+ | in a float number. Since now the division is performed | ||
+ | | ||
+ | | ||
+ | is perform between two floats. The result is 0.666667. */ | ||
+ | result = (float)x/y; | ||
+ | printf(" | ||
+ | |||
+ | /* As before, in a division between an integer and a float value, | ||
+ | the integer value is converted in a float and the division is | ||
+ | | ||
+ | result = x/z; | ||
+ | printf(" | ||
+ | | ||
+ | /* The division x/y is a division between integer numbers (result is 0), | ||
+ | the value 0 is then converted in a float value (0.000000), and then it | ||
+ | is multiplied by x (which is converted into a float number | ||
+ | | ||
+ | The final result is 0.000000 */ | ||
+ | result = (float)(x/ | ||
+ | printf(" | ||
+ | |||
+ | /* In this case, the content of the variable x inside brackets is | ||
+ | | ||
+ | | ||
+ | | ||
+ | is performed between float numbers because one operand is a float. | ||
+ | The result is 1.333333. */ | ||
+ | result = ((float)x/ | ||
+ | printf(" | ||
+ | |||
+ | /* In this last case, the result of the operation between the outer brackets | ||
+ | is 1.333333 as in the previous operation (H:). This number is then converted | ||
+ | in a int value and then saved in the result variable that is of type float. | ||
+ | As a consequence, | ||
+ | | ||
+ | result = (int) ( ((float)x/ | ||
+ | printf(" | ||
+ | | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Output: | ||
+ | The output of this program is: | ||
+ | <code bash> | ||
+ | A: 0.000000 | ||
+ | B: 0.666667 | ||
+ | C: 0.666667 | ||
+ | D: 0.000000 | ||
+ | E: 0.666667 | ||
+ | F: 0.666667 | ||
+ | G: 0.000000 | ||
+ | H: 1.333333 | ||
+ | I: 1.000000 | ||
+ | </ | ||
+ | |||