The Type int

Safe Automatic Conversions

Some automatic conversions do not present any problems. For example:

int n = 12;
double t = n+7;

In this example, the expression n+7 will evaluate to value 19 of type int. This value will be safely converted to type double by automatic conversion. The value 19 of type double is commonly written as 19.0.

Similarly, in:

int m = 12;
double t2 = m + 4.4;

the value 12 of type int will be converted into the value 12 of type double before the operation of addition is performed. The result of addition will be the value 16.4 of type double, which will then be assigned to the variable t2.

Type of an Expression

In order for the compiler to be able to determine whether a value of a correct type is being assigned to a variable, or to produce an error when the types do not match, or to perform an automatic type conversion, it has to know the type of every value while translating a program. Therefore, in the C++ language, the type of every variable, value and expression has to be known during compile-time, which means during the translation of the program by a compiler.

For instance, in the most recent two examples the expression n+7 is of type int, as it is certain to produce a value of type int. It consists of two simple sub-expressions. The sub-expression n is of type int, and the sub-expression 7 is of type int, too.

The expression m + 4.4 is of type double, as it is certain to produce a value of type double, no matter what. It consists of two simple sub-expressions. The sub-expression m is of type int, while the sub-expression 4.4 is of type double.