C has family of integer types i.e {short, int, long, long long}. Any new programmer is likely to use int to represent integer variable in the application and since int type has 32 bit space with range (-2,147,483,648 to 2,147,483,647), there will be bug as soon value of the variable goes out of the range. As you can see the maximum value is 2,147,483,647 which IMHO very small (cannot even count Earth's population).

So my question is how does newbie avoid such bug? What is space of int type on 64 bit OS?

  • Reasons for down vote? Is newbie supposed to be aware of space allocated to types? What happens when he changes machines/OS?
    – vcp
    May 11, 2016 at 10:23
  • 1
    Probably lack of research. It's very easy to google this, and as google will quickly tell you, if you care about the exact range you shouldn't be using variable-sized integral types anyway.
    – Ixrec
    May 11, 2016 at 10:33
  • Of course I would Google for it, I am thinking about students, newbies, how to teach them to avoid such mistake. Anyway ...
    – vcp
    May 11, 2016 at 10:38

5 Answers 5


Note: 'int' is only guaranteed to be at least 16 bits. It's even smaller than you thought! If you want to guarantee at least 32-bits use 'long'. For even larger values look at things like 'int64_t' or 'long long'.

How does a newbie avoid problems like this? I'm afraid it's the same as for many other programming problems. "think carefully and take care".

Running a test at program startup is a good idea. As is having a good set of unit tests. Take extra care when moving to a new platform.

  • 1
    What if it is an 8-bit processor? I don't think that this answer works.
    – Snoop
    May 27, 2016 at 10:21
  • If it is standard compliant C (ie. compliant to the ISO standard) then an int must be large enough to hold at least 16 bits (it's actually expressed as a range of values). Even 8 bit processors must meet this criteria if they are going to "really" implement C. I'm sure some embedded compilers only implement "C-like" languages, but if you use them and rely on their oddities you throw away the portability guarantees that the C Standard gives you. May 28, 2016 at 12:38

Limits.h stores the min and max values for integer types in C.

N.B. C++ has its own version: <limits>

If you're really interested in the number of bits a type uses on your platform, you can do something like this (from here):

#include <limits.h>
#include <stdio.h>

int main(void) {
    printf("short is %d bits\n",     CHAR_BIT * sizeof( short )   );
    printf("int is %d bits\n",       CHAR_BIT * sizeof( int  )    );
    printf("long is %d bits\n",      CHAR_BIT * sizeof( long )    );
    printf("long long is %d bits\n", CHAR_BIT * sizeof(long long) );
    return 0;

As mentioned elsewhere, limits.h will specify the ranges allowed for each type - INT_MIN, INT_MAX, UINT_MAX, etc.

If you need an integer type of a specific width, the stdint.h header provides type definitions like int8_t, int16_t, etc.


We can classify computation in several categories.

For one: A significant context for computing is some form of indexing. We can index into an array, for example. Another form is that we can count objects (e.g. give them an int id) that we create in memory.

For these, our programming languages are usually implemented so that they run out of memory before the simple integer type overflows. For example, arrays may be limited to 32k in 16-bit worlds (and you could only have one of those), 2^31 elements in 32-bit worlds. And you cannot create more than 32k objects in 16-bit worlds and 2^31 object in 32-bit worlds (because objects almost universally use more than one byte each). We can consider that in some sense, larger and large int values imply more memory usage, and that memory is exhausted just before simple int's overflow.

For another, however, we have kinds of computation that doesn't use additional memory in proportion with larger numeric values, such as, summation, we need to be very careful about the selection of data type. Other examples would be rows in a database, where the row number (row id) could exceed memory size as much of the data lives on disc instead of in memory. In these cases we have to look to other limits to indicate the proper size to use. For example, if the db limits to 2^63 rows (big db!), then we should be ok to use a 64-bit int regardless of if we're using a 16-bit machine or larger.


This is best explanation I found on page 3 that any newbie can understand quickly and avoid bugs.

enter image description here

  • 2
    I'd personally be inclined to use the appropriate integral type that has the range I'd need on any platform rather than shotgun the code with custom types. As Nick outlined, there are minimum size guarantees in the C standard.
    – Robbie Dee
    May 11, 2016 at 11:42
  • 5
    These seems like somewhat archaic advice. Nowadays, one would just include stdint.h and use the types defined there. May 11, 2016 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.