How big number of INT can we use?

S

SkyBlue

A straight forward Q from C newbie:
What is the limit of INT variable type?

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).
 
V

Vladimir S. Oka

SkyBlue said:
A straight forward Q from C newbie:
What is the limit of INT variable type?

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).

#include <limits.h>

int largest_int = INT_MAX;

You can browse <limits.h> for other limits as well.
 
C

Charles Richmond

SkyBlue said:
A straight forward Q from C newbie:
What is the limit of INT variable type?

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).
Officially, an "int" is *no* shorter than a "short" and *no* longer
than a "long". And I don't care *who* you tell...
 
V

Vladimir S. Oka

Charles said:
Officially, an "int" is *no* shorter than a "short" and *no* longer
than a "long".

True, but that does not help the OP. If you don't want (or know how) to
give sensible and useful answers to sensible questions, please don't
bother at all.
And I don't care *who* you tell...

???
 
K

Keith Thompson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top