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.
//Specifying a function return type: int sum() { // Creating an integer variable: int number; double price = 19.50; // Casting compatible types to use them as an int. // In this instance you can use the integer part from the double varialbe: number = (int) price; printf("%d\n", number); // Do other stuff here ... // Return an int value from the variable "number" return number; }
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:
sizeof(int);
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).
int number; signed int number; signed number;
unsigned number; unsigned int number;
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.
short number; short int number; signed short number; signed short int number;
long number; long int number; signed long number; signed long int number;
Since C99 a new modifier exists – long long. Normally it holds at least 64bits. Here are examples for declaring variables with this type:
long long number; signed long long number2; long long int number3;
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:
sizeof(int);
The C99 standard adds new types with fixed size and adds the long long modifier.