int in C is a keyword for an integer data type. We use it to create variables, specify function return type or cast to int.
Advertise on this site. I promise you will like the rates :)
The size of the int in C is not fixed. Instead the standard requires that it can hold at least the range from -32767 to 32767. This means the size of the int type is at least 16 bits (2 bytes).
In practice its size depends on the compiler and the machine. Most of the compilers use a 16 bit int for 16 bit (and 8 bit) machines and 32 bit for the rest. Other compilers will match the 16 bit requirement for the older machines and use 32b, 64 etc. for the new 32, 64 bit machines.
The only way to be sure what is the size of the int in your program, use:
Note that it will return the result in bytes (2-4-8).
We can use several modifiers with the numeric types. In C, by default all data is int. If we omit the data type and place only the modifier, it is assumed to be int.
These modifiers change the interpretation for the most significant bit(MSB).
The size modifiers are short and long. They may change the size. I say “may”, because their size is not fixed, too. The standard says that long is at least the size of the int, the int is at least the size of short.
Since C99 a new modifier exists – long long. Normally it holds at least 64bits. Here are examples for declaring variables with this type:
Because of the uncertainty of the int sizes in C, the new standard defined a set of new types and values in <stdint.h>.
The once that guarantee the data size are:
The data type int in C is the natural way to work with integer numbers. Originally it was designed to match the word size of the platform. Its size varies, depending on the compiler and the machine. We can use different modifiers to make it signed, unsigned or increase/decrease its range with short and long.
If you need to know the exact size of the int in a program, use:
The C99 standard adds new types with fixed size and adds the long long modifier.