Section 4 - Type Compatibility

2B.4.1 Compatibility Within the Two Numeric Types

Generally, all integer types (short, int, long) can be mixed, as can both float types (float, double). In other words, you can assign one int type to another or one float type to another.

Note 1) You can put a smaller (lower) type into a larger (upper) type without any fanfare:

short s;
long l;

s = 3;
l = s;          // no cast needed to convert up, or "widen"

This is called "data widening."

Note 2) You must add extra notation if you wish to convert a larger type into a smaller one:

s = (short)l;   // cast needed to convert down or "narrow"

This is called "data narrowing."

This extra notation -- placing the target data type in front of the value on the RHS. (right hand side) of the expression -- is called a cast. You are casting the long l to a short s.

Casting reminds you that you may lose data precision because you are throwing away accuracy. Since putting a float value into a double variable doesn't cost accuracy, there is no need to do anything special.  

Terminology:

Sometimes casting is called coercion or type coercion.

Here is an example:

float x;
double y, ans;
short s;
long l;

y = 1.0;
x = (float)y;   // cast needed to convert "downward"
y = x;          // no cast needed to convert up

l = 123;
s = (short)l;   // cast needed to convert "downward"
l = s;          // no cast needed to convert up

l = (long)y;     // cast needed to convert "downward"
x = l;
Reminder: 

You will lose points if you use a single letter variable name in most situations. 

You see me using single letter names here because I am trying not to confuse you with words in these early examples.  However, in actual practice and in your assignments you would use names like year instead of y, or age instead of x.  The name of the variable should reflect what role it plays in your program.

2B.4.2 Compatibility Between the Two Numeric Types

There is a certain amount of compatibility between floating point  types and integer types. In expressions like
 
int n;
float x;

(2 + (x + n)) / (1.5 - n)

whenever a single binary operation has two numeric types to be mixed, the compiler promotes the lower type to the higher type: int gets promoted (temporarily) to a float, or a float could get promoted to a double.

decorative skyscraper

That's half the story. In the assignment statement:

n = (2 + (x + n)) / (1.5 - n);

the expression on the right evaluates to a float because of promotion, but then it has to get stored in an int variable, n. So the fractional part is tossed out and the number is placed into n as an int.

If it had been

x = n + 5;

then the right hand side of the assignment would evaluate to an int, after which it would  be automatically converted to a float for storage into x.

What do you think would happen in this case (assuming n to be an int and x to be a float)?

n = 5;
x = n / 2;

2B.4.3 Ints and Chars

chars are just short integers -- they are integers  that have only one byte for their storage. Even though we introduced chars as ASCII codes for printable characters, we could, in fact, consider chars as small ints:

char p;

p = 65;
cout << "The answer is  " << p << endl;

As long as the number is between 0 and 255, (approx) it can fit into a char. However, regardless of how we establish the assignment, through an integer, or through a character, when printed out, C++ will show it as a character:

char p;

p = 65;
cout << "The answer is  " << p << endl;

p = 'c';
cout << "The answer is  " << p << endl;

producing output:

The answer is  A
The answer is  c

Just as chars always print out as letters, so ints always print out as values. You can use this as a way to print out some ASCII values, as we show in the next section. (Since ASCII and Unicode are equivalent for the standard character set, we will use these two terms interchangeably).

int n;

n = 'A';
cout << "The answer is  " << n << endl;

producing:

The answer is  65

So, when I speak of an integer data type, I include the char type since it is really closer in form to an int than it is to a String.