How to find the size of an int[] in C/C++?

In C programming, finding the size of a statically declared int[] array means determining how many elements it contains. This is different from finding the memory size − we want the element count. The sizeof operator is the primary method for this in C.

Syntax

int array_length = sizeof(array) / sizeof(array[0]);

Method 1: Using sizeof Operator

The sizeof operator returns the total memory occupied by an array in bytes. Dividing this by the size of one element gives us the number of elements. This works only for statically declared arrays −

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    
    int total_size = sizeof(arr);
    int element_size = sizeof(arr[0]);
    int length = total_size / element_size;
    
    printf("Total memory: %d bytes\n", total_size);
    printf("Size per element: %d bytes\n", element_size);
    printf("Number of elements: %d\n", length);
    
    return 0;
}
Total memory: 20 bytes
Size per element: 4 bytes
Number of elements: 5

Method 2: Creating a Macro

We can create a reusable macro to simplify the calculation −

#include <stdio.h>

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))

int main() {
    int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int values[] = {100, 200, 300};
    
    printf("Size of numbers array: %d\n", ARRAY_SIZE(numbers));
    printf("Size of values array: %d\n", ARRAY_SIZE(values));
    
    return 0;
}
Size of numbers array: 8
Size of values array: 3

Important Limitations

The sizeof method only works with statically declared arrays. It does not work with pointers or dynamically allocated arrays −

#include <stdio.h>
#include <stdlib.h>

void printArraySize(int arr[]) {
    /* This will NOT work correctly - arr is a pointer here */
    printf("Size inside function: %d\n", (int)(sizeof(arr) / sizeof(arr[0])));
}

int main() {
    int static_array[] = {1, 2, 3, 4, 5};
    int *dynamic_array = malloc(5 * sizeof(int));
    
    printf("Static array size: %d\n", (int)(sizeof(static_array) / sizeof(static_array[0])));
    printArraySize(static_array);
    
    free(dynamic_array);
    return 0;
}
Static array size: 5
Size inside function: 2

Key Points

  • sizeof returns bytes, not element count
  • Only works with statically declared arrays in the same scope
  • Arrays passed to functions become pointers − size calculation fails
  • Time complexity: O(1) − calculated at compile time

Conclusion

Finding the size of an int[] array in C is done using sizeof(array) / sizeof(array[0]). This method only works for statically declared arrays and provides an efficient way to determine element count at compile time.

Nishu Kumari
Nishu Kumari

Technical Content Engineer

Updated on: 2026-03-15T10:32:56+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements