Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Why does integer act as long integer?

From what I know an integer is in the range of 32,768 to 32,767. A long integer is in the range of 2,147,483,648 to 2,147,483,647. I doubledchecked wikipedeia to make sure.

The problem now:

int a=2147483647;
printf("a: %d\n", a);

Why does this work? If I add 1 to 2147483647 then it prints garbage, something to be expected if the variable is a long integer. But why does it allow me to assign a long integer number to an integer in the first place?

like image 447
Pithikos Avatar asked Jul 02 '26 13:07

Pithikos


1 Answers

From what I know an integer is in the range of 32,768 to 32,767.

This is incorrect. The range of int is at least -32,767 to 32,767; it might have greater range and on most 32-bit and 64-bit platforms it does.

You can find out the range of the int type on your platform by checking the INT_MAX and INT_MIN macros defined in <limits.h>.

like image 92
James McNellis Avatar answered Jul 05 '26 04:07

James McNellis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!