C int Data Type
In C, the int data type is a primary data type used to store integer values. It is one of the most commonly used types for numeric computations and can store both positive and negative whole numbers.
1 Storage Size of int Data Type
The storage size of the int data type depends on the system and compiler being used. Typically:
| Type | Storage Size |
|---|---|
int | 2 or 4 bytes |
On older 16-bit systems, an int generally occupies 2 bytes, whereas on modern 32-bit and 64-bit systems, it typically occupies 4 bytes.
2 Values Stored by int Data Type
The int data type stores whole numbers, including both positive and negative numbers. It cannot store decimal values or fractions.
Example values that can be stored in int:
10, -50, 1000, 22767, -12718
3 Example: Declaring and Using int Variables
Let’s see a simple program demonstrating how to declare and use int variables in C.
main.c
#include <stdio.h>
int main() {
int num1 = 25;
int num2 = -100;
int sum = num1 + num2;
printf("Number 1: %d\n", num1);
printf("Number 2: %d\n", num2);
printf("Sum: %d\n", sum);
return 0;
}
Explanation:
- We declare three
intvariables:num1,num2, andsum. - We assign values 25 and -100 to
num1andnum2respectively. - The sum of these two numbers is stored in the
sumvariable. - We print the values of
num1,num2, andsumusingprintf().
Output:
Number 1: 25
Number 2: -100
Sum: -75
4 Checking Storage Size of int Programmatically
We can determine the storage size of int in bytes using the sizeof operator.
main.c
#include <stdio.h>
int main() {
printf("Size of int: %lu bytes\n", sizeof(int));
return 0;
}
Output (varies based on system architecture):
Size of int: 4 bytes
5 Minimum and Maximum Values of int
The range of values an int can store depends on its size:
| Storage Size | Minimum Value | Maximum Value |
|---|---|---|
| 2 bytes | -32,768 | 32,767 |
| 4 bytes | -2,147,483,648 | 2,147,483,647 |
6 Getting Maximum and Minimum Values of int Programmatically
The maximum and minimum values of an int can be retrieved using limits.h.
main.c
#include <stdio.h>
#include <limits.h>
int main() {
printf("Minimum int value: %d\n", INT_MIN);
printf("Maximum int value: %d\n", INT_MAX);
return 0;
}
Output:
Minimum int value: -2147483648
Maximum int value: 2147483647
Conclusion
In this tutorial, we explored the int data type in C, including:
- Its ability to store whole numbers (both positive and negative).
- Its typical storage size of 2 or 4 bytes, depending on the system.
- How to get the storage size programmatically using
sizeof(). - The minimum and maximum values that
intcan store. - How to retrieve these values using
limits.h.
