Reputation: 1340
After numerous research on the web, I couldn't figure out if a conversion from int to long is considered as a promotion or not.
If found the following code on the Microsoft's website here :
long long_num1, long_num2;
int int_num;
// int_num promoted to type long prior to assignment.
long_num1 = int_num;
However it's not indicated on the cppreference website.
I understand promotion is a conversion that is value-keeping (doesn't change the value when converting).
Could someone help me figure this out?
Thanks in advance
Upvotes: 3
Views: 883
Reputation: 1048
As far as I know, there is no data loss, as int
and long
have both the same size of 4 bytes. You can check whether your compiler agrees with this by running the following code:
std::cout << sizeof(int) << ' ' << sizeof(long);
The output should be "4 4". That would mean they are both the same.
EDIT: Sorry; thanks for correcting me. The sizes may vary, use a sizeof
whenever referencing them.
Upvotes: 0
Reputation: 140970
long long_num1, long_num2; int int_num; // int_num promoted to type long prior to assignment. long_num1 = int_num;
The text is incorrect. int_num
is not promoted, because it's already an int
, so no point applies here.
On assignment, a implicit conversion happens - from source type to destination type. Because long
can represent all values of int
, the value is not changed and int_num
value is converted to a long
with the same value.
Upvotes: 2