previous | index | next

Assignments, Types, and Casts

If an assignment statement's expression value is not compatible with the variable's type, as in:
      int p = 1.0;
the C++ compiler may complain:
      warning: converting to `int' from `double'
While in this case there is no loss of accuracy, in other cases there is:
      int p = 1.5;  // p gets value 1 -- no rounding is done
You can deliberately convert a value from one type to another by casting it, like this:
      int p = (int) 1.5;
Now the compiler warning will not be given.

You can also use a more appropriate type:

      double p = 1.5;
Now there are no type casts and no warnings.

previous | index | next