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 be considered unharmful to assign directly?longis not a different data type, but merely a modifier, - 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?