User Tools

Site Tools


Action disabled: revisions
cs:c_language:casting
Return to Home page

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 two operands of an expression are converted to the type with higher rank.

The ranks of C types, in increasing rank order are the following:

  1. _Bool
  2. char
  3. short or short int
  4. unsigned short or unsigned short int
  5. int
  6. unsigned int
  7. long or long int
  8. unsigned long or unsigned long int
  9. long long or long long int
  10. unsigned long long or unsigned long long int
  11. float
  12. double
  13. long double

As an example, the following operation:

3.5+4;

is an operation between a float and an integer value, as a consequence the integer value is converted in a float value (i.e., 4.000000) and the operation is actually performed between two float numbers obtaining as result the value 7.500000.

The same concept holds also in the case variables are involved in a mathematical expression. For example, the following code:

int a;
float result;
a=4;
result = (3.6+a)*2;

stores in the variable result the value 15.20000 because the first operation (3.6+a) is performed between float numbers, indeed an operand (i.e., 3.6) is a float value and the variable a, which is an integer variable, is converted in a float number to perform the operation. The result of the operation (3.6+a) is the float number 7.200000. Once again the number 2 is converted into a float because one operand (i.e., 7.200000) is a float. At the end the variable result will contain the value 15.200000.

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:

float result;
int x=2, y=3;
result = ((float)x/y) * x;

the division between x/y without the casting operator will give 0 as result, because is a division between two integer variables. With the casting operator before the variable x (i.e., (float)x), the value contained in the variable x is transformed in a float value (2.000000), therefore the division that contains a float operand is performed between floats and the result is 0.666666. Once again the float value 0.666666 is multiplied by 2 obtaining as a result the value 1.333333 with is stored in the variable result.

The same operation but with the casting operator outside the brackets:

result = (float)(x/y) * x;

stores in the variable result the value 0.000000, because from the division x/y the value 0 is obtained, which is converted by the casting operator into a float value (0.000000), and in turn it is multiplied by 2, obtaining finally 0.000000.

Solution:

casting.c
/* Some examples about mathematical expressions and casting */
 
#include <stdio.h>
 
 
int main() {
 
  int x = 2;
  int y = 3;
  float z = 3;
 
  float result;
 
  /* Division between integer numbers, the result is the integer
     number 0, then converted in the float number 0.000000 for the
     assignment to the variable result */
  result = 2/3;
  printf("A: %f\n", result);
 
  /* Division between an integer and a float number, the integer
     number is converted in a float number, and the operation
     is perform between two floats. The result is 0.666667. */
  result = 2.0/3;
  printf("B: %f\n", result);
 
  /* The same as the last operation. The number 3. is considered
     a float number */
  result = 2/3.;
  printf("C: %f\n", result);
 
  /* Division between integer variable, the result is the integer
     number 0, then converted in the float number 0.000000 for then
     assignment */
  result = x/y;
  printf("D: %f\n", result);
 
  /* The casting operator (float) converts the content of the variable x
     in a float number. Since now the division is performed 
     between a float and an integer number, the integer
     number is converted in a float number, and the operation
     is perform between two floats. The result is 0.666667. */
  result = (float)x/y;
  printf("E: %f\n", result);
 
  /* As before, in a division between an integer and a float value,
     the integer value is converted in a float and the division is
     performed between two floats. */
  result = x/z;
  printf("F: %f\n", result);
 
  /* 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
     because the other operator (float)(x/y) is a float number).
     The final result is 0.000000 */
  result = (float)(x/y) * x;
  printf("G: %f\n", result);
 
  /* In this case, the content of the variable x inside brackets is 
     converted in a float number, for this reason the division is performed
     between float numbers, obtaining a float result. The 
     result of the division is multiplied with x and, once again, the operation
     is performed between float numbers because one operand is a float.
     The result is 1.333333. */
  result = ((float)x/y) * x;
  printf("H: %f\n", result);
 
  /* 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, the content of the variable result is 1.000000. */
 
  result = (int) ( ((float)x/y) * x );
  printf("I: %f\n", result);
 
 
  return 0;
}

Output:
The output of this program is:

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

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/casting?do=revisions
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/casting.txt · Last modified: 2020/11/26 23:18 (external edit)