I was experimenting in C about the range of different datatypes and I stumbled into this problem. We know that the max value of int datatype is 2147483647. So I tried to assign a larger value to the int data type which was 21474836481234. It was way larger number than the maximum value of stored in int datatype. So when I printed the the output which came as "1234". I didn't understand how this number was printed. Can anyone explain to me? Thank You!
#include <stdio.h>
#include <limits.h>
void main()
{
int a;
a=214748364812323;
printf("a= %d",a);
}
CodePudding user response:
It was way larger number than the maximum value of stored in int datatype . So when I printed the the output which came as "1234".I didn't understand how this number was printed.
The hexadecimal representation of 21474836481234 is 0x1388000004D2, which means that the low 32 bits is 000004D2, i.e. 1234 in decimal. When you assigned 0x1388000004D2 to a type that can only store 32 bits, it's the low 32 bits that get stored, so you end up with 000004D2.
Try doing the same thing with a smaller type. char is only 8 bits, so try storing a larger value like 0x10A and you'll see that the resulting value is only 0x0A.
CodePudding user response:
When given an out of range value for a signed type, "the result is implementation-defined or an implementation-defined signal is raised."
This setup gets 1234 for 21474836481234 and 12323 for 214748364812323. These are the int values produced by treating the least significant 32 bits of the inputs as int values.
max value of int datatype is 2147483647
That may be true for your program, but it's not true in general. The standard only guarantees that the range will be at least -32,767 .. 32,767 and at least the range of a signed short int (and of a signed char).
The Wikipedia page about C data types is quite good.
CodePudding user response:
So when I printed the the output which came as "1234".I didn't understand how this number was printed. Can anyone explain to me?
214748364812323 is a valid constant, yet outside OP's int range, perhasp it is long or long long.
Assigning that wider than int type to an int incurs:
Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised. C17dr § 6.3.1.3 3
a took on some value. OP reports 1234 for the constant 214748364812324 - That is valid C. Often it is the lower bits of the value, but it may differ on other implementations.
Save time. Enable all compilers warnings to be alerted to troublesome code like int a; a=214748364812323;
CodePudding user response:
This is undefined behavior in c. For fun I tried in gcc and got the following warning:
t.c: In function ‘main’:
t.c:5:1: warning: overflow in implicit constant conversion [-Woverflow]
a=214748364812323;
This is the compiler telling me that an overflow is occurring when trying to assign the value to a.
I get 12323 as the result.
Decimal 214748364812323 = 0xC35000003023. int on my system is 32 bits, so it looks like gcc only assigned the least significant 32 bits of the value 0x00003023, which would be typical overflow behavior.
