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:

TypeStorage Size
int2 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

</>
Copy
#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:

  1. We declare three int variables: num1, num2, and sum.
  2. We assign values 25 and -100 to num1 and num2 respectively.
  3. The sum of these two numbers is stored in the sum variable.
  4. We print the values of num1, num2, and sum using printf().

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

</>
Copy
#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 SizeMinimum ValueMaximum Value
2 bytes-32,76832,767
4 bytes-2,147,483,6482,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

</>
Copy
#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:

  1. Its ability to store whole numbers (both positive and negative).
  2. Its typical storage size of 2 or 4 bytes, depending on the system.
  3. How to get the storage size programmatically using sizeof().
  4. The minimum and maximum values that int can store.
  5. How to retrieve these values using limits.h.