SkyBlue said:
A straight forward Q from C newbie:
What is the limit of INT variable type?
There is no type INT. There is a type int, which I presume is what
you're referring to. This isn't a trivial distinction; C is
case-sensitive, and you could declare a type INT if you really wanted
to (though it would be a bad idea).
The maximum possible value of INT is specified by INT_MAX, a macro
defined in <limits.h>. The standard guarantees that INT_MAX is at
least 32767 (2**15-1).
If you want to store bigger values than that, you can use long (upper
bound is LONG_MAX, guaranteed to be at least 2147483647, or 2**31-1)
or a long long (upper bound is LLONG_MAX, guaranteed to be at least
9223372036854775807, or 2**63-1); the latter is new in C99, and may or
may not be supported by your implementation.
I'm writing a code to calculate the sum of odd/even numbers between a
given range, and I want to limit the number size so it works (e.g, give
an error message if the number given is too big).
To do that, you'll need to do more than compare the value against
INT_MAX. Once a value overflows, it's too late to check it. Checking
whether a calculation is *going* to overflow is more difficult.