In a recent homework assignment I’ve been told to use long variable to store a result, since it may be a big number.
I decided to check will it really matter for me, on my system (intel core i5/64-bit windows 7/gnu gcc compiler) and found out that the following code:
printf("sizeof(char) => %d\n", sizeof(char));
printf("sizeof(short) => %d\n", sizeof(short));
printf("sizeof(short int) => %d\n", sizeof(short int));
printf("sizeof(int) => %d\n", sizeof(int));
printf("sizeof(long) => %d\n", sizeof(long));
printf("sizeof(long int) => %d\n", sizeof(long int));
printf("sizeof(long long) => %d\n", sizeof(long long));
printf("sizeof(long long int) => %d\n", sizeof(long long int));
produces the following output:
sizeof(char) => 1
sizeof(short) => 2
sizeof(short int) => 2
sizeof(int) => 4
sizeof(long) => 4
sizeof(long int) => 4
sizeof(long long) => 8
sizeof(long long int) => 8
In other words, on my system, int and long are the same, and whatever will be too big for int to hold, will be too big for long to hold as well.
The homework assignment itself is not the issue here. I wonder how, on a system where int < long, should I assign an int to long?
I’m aware to the fact that there are numerous closely related questions on this subject, but I feel that the answers within these do not provide me with the complete understanding of what will or may happen in the process.
Basically I’m trying to figure out the following:
- Should I cast
longtointbefore the assignment, orsinceit will belong
is not a different data type, but merely a modifier,
considered unharmful to assign directly? - What happens on systems where
long > int? Will the result be
undefined (or unpredictable) or it will cause the extra parts of the
variable to be omitted? - How does the casting from
longtointworks in C? - How does the assignment from
longtointworks in C when I don’t
use casting?
The language guarantees that
intis at least 16 bits,longis at least 32 bits, andlongcan represent at least all the values thatintcan represent.If you assign a
longvalue to anintobject, it will be implicitly converted. There’s no need for an explicit cast; it would merely specify the same conversion that’s going to happen anyway.On your system, where
intandlonghappen to have the same size and range, the conversion is trivial; it simply copies the value.On a system where
longis wider thanint, if the value won’t fit in anint, then the result of the conversion is implementation-defined. (Or, starting in C99, it can raise an implementation-defined signal, but I don’t know of any compilers that actually do that.) What typically happens is that the high-order bits are discarded, but you shouldn’t depend on that. (The rules are different for unsigned types; the result of converting a signed or unsigned integer to an unsigned type is well defined.)If you need to safely assign a
longvalue to anintobject, you can check that it will fit before doing the assignment:The details of “something else” are going to depend on what you want to do.
One correction:
intandlongare always distinct types, even if they happen to have the same size and representation. Arithmetic types are freely convertible, so this often doesn’t make any difference, but for exampleint*andlong*are distinct and incompatible types; you can’t assign along*to anint*, or vice versa, without an explicit (and potentially dangerous) cast.And if you find yourself needing to convert a
longvalue toint, the first thing you should do is reconsider your code’s design. Sometimes such conversions are necessary, but more often they’re a sign that theintto which you’re assigning should have been defined as alongin the first place.