ikeeki
ikeeki

Reputation: 69

Why can I operate with int > +32767?

I can read that int range (signed) is from [−32767, +32767] but I can say, for example

int a=70000;
int b=71000;
int c=a+b;

printf("%i", c);
return 0;

And the output is 141000 (correct). Should not the debugger tell me "this operation is out of range" or something similar?

I suppose that this has to be with me ignoring the basics of C programming, but none of the books that I'm currently reading tell nothing about this "issue".

EDIT: 2147483647 seems to be the upper limit, thank you. If a sum exceeds that number, the result is negative, wich is expected, BUT if it is a subtraction, for example: 2147483649-2147483647=2 the result is still good. I mean, why the value 2147483649 is correctly hold for that substraction purpose (or at least it seems to me)?

Upvotes: 5

Views: 3219

Answers (7)

kometen
kometen

Reputation: 7832

If an int is four bytes an unsigned is 4294967295, signed max. 2147483647 and signed min. -2147483648

unsigned int ui = ~0;
int max = ui>>1;
int min = ~max;
int size = sizeof(max);

Upvotes: 1

Gustavo Toyota
Gustavo Toyota

Reputation: 33

The size of an int (and the max value it can hold) depends on the compiler and the computer you are using. There is no guarantee that it will have 2 bytes or 4 bytes, but there is a guaranteed minimum size for the c++ variables.

You can see a list of minimum sizes for c++ types in this page: http://www.cplusplus.com/doc/tutorial/variables/

Upvotes: 0

TemplateRex
TemplateRex

Reputation: 70556

In C++, int is at least 16-bits long, but typically 32-bits on modern hardware. You can write INT_MIN and INT_MAX and check yourself.

Note that signed integer overflow is undefined behavior, you are not guaranteed to get a warning, except perhaps with high compiler warnings and debug mode.

Upvotes: 4

You have misunderstood. The standard guarantees that a int holds [-32767, +32767], but it is permitted to hold more. (In particular, nearly every compiler you are likely to use allows a range [-2147483648, 2147483647]).

There is another problem. If you make the value you assign to a and b bigger you still probably won't get any warning or error. Integer overflow causes "undefined behaviour", and literally anything is allowed to happen.

Upvotes: 3

adjan
adjan

Reputation: 13684

While the standard guarantees the size of int to be 16 bit, it is usually implemented as a 32-bit value.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409404

All types are compiler-dependent. int used to be the "native word" of the underlying hardware, which on 16-bit systems meant that int was 16 bits (which leads to the -32k to +32k range). When 32-bit systems started coming then int naturally followed along and became 32 bits, which can store values around -2 billion to +2 billion.

However this "native word" use for int didn't follow along when 64-bit systems came around, I know of no 64-bit system or compiler that have int being 64 bits.

See e.g. this reference of integer types for more information.

Upvotes: 5

Bo Persson
Bo Persson

Reputation: 92341

The range [−32767, +32767] is the required minimum range. An implementation is allowed to provide a larger range.

Upvotes: 6

Related Questions