The Type int
Automatic Conversion
A variable of type int
cannot
contain anything but an integer. If an attempt is made to assign some other
value to it, such as
int y = 6.73;
then the assignment should fail and produce a syntax error. Although it does not fail in this case, actually.
The authors of the C programming language
allowed a value of type double
to be automatically converted into the type int
. The conversion will just remove the decimal part of the number and leave the whole
part only. Back in the old days, this seemed to be a convenient feature of the
language. Nowadays, it seems to be a bad language design choice. To make up for
it, modern compilers issue a warning during the translation process that an
unsafe type conversion is taking place.
To see this warning, from the menu select BUILD -> Rebuild Solution. This is what the compiler might say:
...\visual studio 2015\projects\myproject1\main.cpp(9): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
"The loss of data" the compiler is referring to is about losing the fractional part of the number. The warning itself will be displayed in the bottom part of your IDE, inside the Output window.
As a consequence of automatic type
conversion, the variable y
will actually contain the value 6.