
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the size of an int[] in C/C++?
In C and C++, arrays can store multiple values of the same data type. This problem is about finding how many elements are present in a statically declared int[] array (not pointers or dynamically allocated arrays). We'll learn how to find the size of an int[] in both C and C++.
Let's understand this with an example:
//Example 1 input: int numbers[] = {10, 20, 30, 40, 50}; This array has 5 elements. Output: Number of elements: 5 //Example 2: Input: int values[] = {1, 2, 3}; This array has 3 elements. Output: Number of elements: 3
We can find the size of a statically declared array (int[]) using the sizeof operator. Below are two ways to do this in C++ and C:
Finding Size of int[] Using sizeof (Only for Static Arrays)
In this approach, we use the sizeof operator to find the total size of the array and divide it by the size of one element. This gives us the number of elements in the array. This method works only for statically declared arrays and not for pointers or dynamically allocated memory.
C++ Example
Here's a complete C++ example where we define a static array and use sizeof(arr) / sizeof(arr[0]) to find how many elements it has and how much memory it uses.
#include <iostream> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5, 9, 23, 1}; // calculating the number of elements in the array int length = sizeof(arr) / sizeof(arr[0]); cout << "Memory occupied by arr[]: " << sizeof(arr) << endl; cout << "Size of the array: " << length << endl; return 0; }
Below is the output of the program that shows the memory occupied by the array and the total size of the array in bytes.
Memory occupied by arr[]: 32 Size of the array: 8
Time Complexity: O(1), because it's just a direct size calculation.
Space Complexity: O(n), where n is the number of elements stored in the array.
C Example
Below is a complete C program that does the same. We define a static array and use the sizeof operator to get the number of elements and the memory it uses.
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; // calculating the number of elements in the array int length = sizeof(arr) / sizeof(arr[0]); int sizee = sizeof(arr); // printing the memory size printf("Memory occupied by arr[]: %d bytes\n", sizee); // printing the size of the array printf("Size of the array: %d\n", length); return 0; }
The output shows the total memory used by the array in bytes and the number of elements it contains.
Memory occupied by arr[]: 20 bytes Size of the array: 5
Time Complexity: O(1), because the calculation is done using static values at compile-time.
Space Complexity: O(1), since no extra memory is used.
Finding Size of int[] Using std::size()
This method uses std::size() from the <iterator> header. It returns the number of elements in a statically declared array and is safer than manually using sizeof.
Example
Below is a complete C++ program where we use std::size(arr) from the C++ Standard Library to directly get the number of elements in a static array.
#include <iostream> #include <iterator> // needed for std::size int main() { int arr[] = {5, 10, 15, 20, 25}; // printing the number of elements using std::size std::cout << "Size of the array: " << std::size(arr) << std::endl; return 0; }
The output displays the number of elements present in the given array using std::size.
Size of the array: 5
Time Complexity: O(1) because the array size is calculated directly.
Space Complexity: O(n) because the array stores n elements in memory.