What is int Data Type?
Integer types can be signed (with negative values) or unsigned values (only positive). Int values are always signed unless specifically mentioned.
Integer types are further classified as –
Data type | Range |
int | |
signed int | −32,768 to 32,767 |
unsigned int | 0 to 65,535 |
short int | |
signed short int | -2,147,483,648 to 2,147,483,647 (4 bytes) |
unsigned short int | 0 to 4,294,967,295 (4 bytes) |
long int | |
signed long int | -2,147,483,648 to 2,147,483,647 (4 bytes) |
unsigned long int | 0 to 4,294,967,295 (4 bytes) |
Some examples:
int number = 456;
long prime = 12230234029;
How to print integer variables? Here is a small program that you can try and tweak to get different results and understand the range of short, int, and long.
#include
int main(void) {
short int num1 = 10000;
int number = 121113991;
long prime = 49929929991;
long notprime = 2300909090909933322;
long long sum = prime + notprime;
printf("num1 is %hd, number is %d, prime is %ld, notprime is %ld, sum is %lld", num1, number, prime, notprime, sum);
return 0;
}
We have used %hd for short, %d for int, and so on for printing each data type.
Note that we have used ‘long long’ for sum, which is 8 bytes, whereas long is 4 bytes. Though in practical situations, we may not use numbers that are this big, it is good to know the range and what data type we should use for programs with exponential calculations. We can use %u in place of %d for unsigned int but even %d works. Let us say the value of long notprime = -2300909090909933322; has a minus, but we print it as notprime is %lu, the correct value will not be printed. This is why it is safe to use %ld, unless you want the values to be always unsigned.If we add more digits to short int num1 = 10000, it will be out of range and will print wrong value. ‘short int’ can be used to limit the size of the integer data type.