I was reading http://www.cplusplus.com/doc/tutorial/typecasting/. It says that:
Otherwise, if the conversion is between numeric types of the same kind (integer-to-integer or floating-to-floating), the conversion is valid, but the value is implementation-specific (and may not be portable).
But I really didn't understand what does the above quote mean to say? Will someone please explain it using an simple example? Why conversion between numeric type of same kind results in implementation-specific value? What is the reason?
The possible types of conversion are standard conversion, no conversion, and user-defined nonstandard conversion.
Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program. It is also called automatic type conversion.
Explicit type conversion, also called type casting, is a type conversion which is explicitly defined within a program (instead of being done automatically according to the rules of the language for implicit type conversion). It is defined by the user in the program.
Typecasting, or type conversion, is a method of changing an entity from one data type to another. It is used in computer programming to ensure variables are correctly processed by a function. An example of typecasting is converting an integer to a string.
Let's consider the following example:
long long int lli = 5000000000;
long int li;
int i;
li = lli;
i = li;
Can you predict the values of lli
, li
and i
? Or whether li
and i
have the same value?
Answer is - values depend on the number of bytes allocated for each type!
I.e. for some cases int
is equal to long int
, for others long int
is equal to long long int
, but in general long
er types just CAN be longer. Similar (in sense of memory size) for float
, double
and long double
.
The snippet refers to narrowing conversions between integral and floating point types respectively. That is, it states that although a conversion between integral types or between floating point types is valid, the resulting value will be implementation defined.
As an example consider the following piece of code:
#include <iostream>
#include <limits>
int main() {
long long lgm = std::numeric_limits<long long>::max();
std::cout << std::hex << lgm << std::endl;
int i = lgm;
std::cout << std::hex << i << std::endl;
long double ldb = std::numeric_limits<long double>::max();
std::cout << std::hex << ldb << std::endl;
double db = ldb;
std::cout << std::hex << db << std::endl;
}
Output:
7fffffffffffffff
ffffffff
1.18973e+4932
inf
As you can see the maximum value of long long
integer exceeds the capacity of a plain integer. However you can convert a long long
to an int
(i.e., the conversion is valid), but due to the fact that the int
can't hold the maximum value of a long long
there cannot be an accurate conversion. Thus, the value of the resulting int
is left to be decided by the implementation. The same holds true for the conversion between long double
and double
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With