Difference Between int and size_t in C++

In C++, both int and size_t are very important data types that are used to represent integers. However, there are some key differences between them that the users should be aware of. In this article, we will learn the differences between int and size_t in C++.

Integer Data Type in C++

  • The int data type in C++ is a signed data type, meaning it can represent both positive and negative values.
  • The size of an int can vary depending on the system architecture, but it is guaranteed to be at least 16 bits (2 bytes). On most modern systems, int is 32 bits (4 bytes).
  • The range of values that an int can represent depends on its size and whether it is signed (can be negative) or unsigned. A signed 32-bit int can typically represent values from -2,147,483,648 to 2,147,483,647.
  • The int data type is generally used to perform arithmetic operations.

Size_t Data Type in C++

  • It is designed to be large enough to represent the maximum size of any object in the given environment.
  • The specific size of size_t depends on the system architecture and compiler implementation. It is typically 32 bits (4 bytes) on 32-bit systems and 64 bits (8 bytes) on 64-bit systems.
  • Since size_t is unsigned, it can only represent non-negative values. The size_t data type can represent values ranging from 0 to 4,294,967,295.

Difference Between int and size_t in C++

Following are some key differences between int and size_t in C++:

Feature

int

size_t

Definition

Signed integer data type

Unsigned integer data type

Header file

Defined in <stdio.h>

Defined in various header files such as: <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, <wchar.h>

Size

Normally 32 bits (but can vary)

Platform-dependent, same as the architecture’s pointer size

Range

Ranges between -2,147,483,648 to 2,147,483,647 (for 32-bit)

Ranges between 0 to 4,294,967,295 or 0 to 18,446,744,073,709,551,615 (as per system)

Negative Values

Can represent negative values

Cannot represent negative values.

Purpose

General integer arithmetic

Representing sizes of objects in memory

Compatibility

Compatible with both positive and negative arithmetic operations

Compatible with only non-negative values.

Usage

Used to store,temperatures, coordinates, loop counters

Used to store sizes of arrays, strings, memory allocations

C++ Program for Using Int

The following program demonstrates the use of int data type to perform arithmetic operations between negative and positive integers in C++:

C++
// C++ Program Using Int to perform addition of a negative and a positive integer
#include <iostream>
using namespace std;

int main()
{
    // use int for negative numbers
    int num = -50;
    int positive = 100;

    // use int for arithmetic operations
    int result = num + positive;
    cout << "Result of adding " << num << " and " << positive << " is " << result << endl;
    return 0;
}

Output
Result of adding -50 and 100 is 50

Time Complexity: O(1)
Auxiliary Space: O(1)

C++ Program for using Size_t

The following program demonstrates the use of size_t data type to store the size of an array in C++:

C++
// C++ Program using Size_t to store the size of an array
#include <iostream>
using namespace std;

int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };

    // use size_t to store the number of elements in the array
    size_t size = sizeof(arr) / sizeof(arr[0]);

    // Iterate through the array using size_t and print the array elements
    cout<<"Array Elements: ";
    for (size_t i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
      cout<<"\nSize of the array is: "<<size;
    return 0;
}

Output
Array Elements: 1 2 3 4 5 
Size of the array is: 5

Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(1)



Contact Us