J
jacob navia
In one of the previous discussions I received a note from
C.B. Falconer indicating me that a cast from long long
to int was unnecessary since the compiler will do that
without the cast automatically.
This is true, but:
Suppose
long long b;
char a;
a = b;
In this assignment, the long long is truncated to a byte. You do
not need a cast in principle to do this, but you are discarding
data when you do this assignment.
I have recently added a warning to the lcc-win32 compiler system
when an assignment like this happens, to warn you about
the implicit data discarding.
To avoid the warning you have to explicitely cast the LHS to meet
the RHS, like this:
a = (char)b;
The cast makes your intentions clear.
The same approach exists in Microsoft compilers.
I have gotten used to do this, more or less automatically when I
cast "down", i.e. from a bigger to a smaller type.
jacob
C.B. Falconer indicating me that a cast from long long
to int was unnecessary since the compiler will do that
without the cast automatically.
This is true, but:
Suppose
long long b;
char a;
a = b;
In this assignment, the long long is truncated to a byte. You do
not need a cast in principle to do this, but you are discarding
data when you do this assignment.
I have recently added a warning to the lcc-win32 compiler system
when an assignment like this happens, to warn you about
the implicit data discarding.
To avoid the warning you have to explicitely cast the LHS to meet
the RHS, like this:
a = (char)b;
The cast makes your intentions clear.
The same approach exists in Microsoft compilers.
I have gotten used to do this, more or less automatically when I
cast "down", i.e. from a bigger to a smaller type.
jacob