Lesson 3.3. Operators and types

Before studying the other C operators, it is important to understand that processors can only perform operations on variables of the same type and size. When the types are different, the compiler will convert them (in a more or less judicious way) so that in the end, the calculation can be done with two operands of the same type. For example: two short or two double.

For the same reason, the result of an operation is of the same type as the operands. For example:

This mechanism can have important consequences, let's analyze the two following examples.

Example 1

Consider the following example:

float a=7, b=2;
printf ("7 / 2 = %f\n", a/b );

The operands a and b are of float type, the result of the division will be a float, hence the format code %f in the printf. Here, no worries, the result of 7 divided by 2 is 3.5.

Example 2

Consider now the following example:

int a=7, b=2;
printf ("7 / 2 = %d\n", a/b );

The operands a and b are of type integer, the result of the division will also be an integer, hence the format code %d in the printf. But beware, an integer cannot contain a decimal number, the result will be truncated to the the lower integer:

7 / 2 = 3
In each operation, you must control the type of the operands.

If the two operands are of different types, the compiler will perform an automatic type conversion. Needless to say, it is better to avoid letting the compiler choose the type of the result. We will see later that it is possible to perform type casting i.e. convert them temporarily to another type.

Exercises

Exercise 1

Write a program that declares 3 variables a, b and x of type float.

#include <stdio.h>

int main(void) {
    // Declare x, a=5 and b=2 (type float)
    // COMPLETE HERE

    // Divide a by b => x
    // COMPLETE HERE

    // Display x
    printf ("x = %f\n", x);

    return 0;
}

Exercise 2

Write a program that declares 3 variables a, b and x of type int.

#include <stdio.h>

int main(void) {
    // Declare x, a=5 and b=2 (type int)
    // COMPLETE HERE

    // Divide a by b => x
    // COMPLETE HERE

    // Display x
    printf ("x = %d\n", x);

    return 0;
}

Quiz

The processors can perform an operation on ...

Check Bravo! Processors can only perform operations on operands of the same type. Try again...

What will be the value contained in the variable x?

float x = 15 / 2;
Check Bravo! The operands are two integers, so the result is an integer. The proof by example. Try again...

What will be the value contained in the variable x?

float x = 15. / 2.;
Check Bravo! The operands are floats, so the result is also a float. The proof by example. Try again...

If the two operands are of different types, what will happen?

Check Bravo! The compiler has automatic type conversion rules. Try again...

When the compiler automatically converts operands of different types ...

Check Good for you! Even if the automatic transtyping rules are well done, they don't always produce the expected result. Try again...

See also


Last update : 11/21/2022