If you have your array in scope you can use sizeof to determine its size in bytes and use the division to calculate the number of elements:

#define NUM_OF_ELEMS 10
int arr[NUM_OF_ELEMS];
size_t NumberOfElements = sizeof(arr)/sizeof(arr[0]);

If you receive an array as a function argument or allocate an array in heap you can not determine its size using the sizeof. You'll have to store/pass the size information somehow to be able to use it:

void DoSomethingWithArray(int* arr, int NumOfElems)
{
    for(int i = 0; i < NumOfElems; ++i) {
        arr[i] = /*...*/
    }
}
Answer from Pavel Zhuravlev on Stack Overflow
🌐
Reddit
reddit.com › r/c_programming › getting the number of element of an array
r/C_Programming on Reddit: Getting the number of element of an array
August 28, 2022 -

hi, I'm new in C. I'm having a hard time finding the number of elements in an int array.
Here's what I'm trying to achieve.
I've defined a macro
#define MAXSZ 30

Then I've declared an array
int arr[MAXSZ];

What I wanted to do, is, taking user input to initialize the array up until the user inputs -1. Then get the number of elements.
int item;
for(int i = 0; i < MAXSZ; i++) {
scanf("%d", &item);
if (item == -1) break;
else arr[i] = item;
}

Now, when I'm trying to get the number of elements.
For input
1
2
3
5
6
-1
It shows 30 elements (as it's supposed to).
Output
30
0x7ffe9be05320 -> 1
0x7ffe9be05324 -> 2
0x7ffe9be05328 -> 3
0x7ffe9be0532c -> 5
0x7ffe9be05330 -> 6
0x7ffe9be05334 -> 32572
0x7ffe9be05338 -> -1293942784
0x7ffe9be0533c -> -182336
0x7ffe9be05340 -> -1381222790
0x7ffe9be05344 -> 32572
0x7ffe9be05348 -> 0
...

My question is, how do I get rid of the garbage values & only get the number of items that has been given as input?

Discussions

Count the number of elements in an array in C - Stack Overflow
Not only can arrays be dynamically ... but as of C99 their size can be dynamic as well -- so-called Variable-Length Arrays (VLAs). 2013-04-10T21:57:55.36Z+00:00 ... You are most likely doing this inside the function to which you pass the array. The array decays as pointer to first element So You cannot ... More on stackoverflow.com
🌐 stackoverflow.com
calc. number of elements in array - C++ Forum
However, std::vector should be preferred. It has a size()-function returning the number of elements. As soon as array to pointer decay ocurres (which will happen really fast), your method won't work anymore. ... i tried this, but sizeof(array) always comes out to be 4, being an integer ... More on cplusplus.com
🌐 cplusplus.com
August 22, 2008
C: finding the number of elements in an array[] - Stack Overflow
In f array is a pointer, in main array is an array. ... And once you have a pointer you can't determine the number of elements. More on stackoverflow.com
🌐 stackoverflow.com
How do I determine the size of my array in C? - Stack Overflow
How do I determine the size of my array in C? That is, the number of elements the array can hold? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Sanfoundry
sanfoundry.com › c-program-number-elements-array
C Program to Find the Number of Elements in an Array - Sanfoundry
May 13, 2022 - 2. Create a variable which will hold the size of the array. 3. Using the inbuilt function sizeof(), passing the name of the array whose size we need to calculate, returns the number of elements contained by array.
🌐
Quora
quora.com › How-can-we-determine-the-number-of-elements-in-an-array-in-C-C-without-using-additional-variables-or-functions
How can we determine the number of elements in an array in C/C++ without using additional variables or functions? - Quora
This will synthesize a “function” for each individual array you send as a parameter. It is not really a function, because the answer is computed at compile time and probably the code is elided and just the size_t is substituted, but it does work. Notice the tortured syntax (&array)[N] to send an array as a reference, that is tricky. At actual runtime there is a facility called std::extent which can do the old trick of multiplying the size of the container by the number of elements.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › length-of-array-in-c
Length of Array in C - GeeksforGeeks
This gives the number of elements in the array, which is 20/4 = 5 · We can also calculate the length of an array in C using pointer arithmetic.
Published   October 17, 2025
🌐
Quora
quora.com › How-can-I-find-the-number-of-elements-in-an-array-in-C-programming
How to find the number of elements in an array in C programming - Quora
Answer (1 of 3): C arrays are inferior objects. They have no markers for size, and no operations that can be performed with them. They are only a contiguous range of bytes. They are of fixed size at declaration, and any tricks used to change the size on the fly are dangerous moves with complicati...
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › c › determine the size of an array in c
Determine the size of an array in C | Sentry
May 15, 2023 - Using sizeof on an array will return the array’s size in bytes, as below: ... The size_t type is used for sizes returned by sizeof. To get the number of elements in our array, we need to divide this value by the size of a single element.
🌐
IONOS
ionos.com › digital guide › websites › web development › c: array length
How to determine the length of an array in C
December 10, 2024 - Total size of the array: 20 bytes Size of a single element: 4 bytes Number of elements in the array: 5c · Pointers them­selves don’t contain in­for­ma­tion about the size or length of an array.
🌐
BeginnersBook
beginnersbook.com › 2017 › 09 › c-program-to-find-the-number-of-elements-in-an-array
C Program to Find the Number of Elements in an Array
September 24, 2017 - #include <stdio.h> int main() { double arr[] = {11, 22, 33, 44, 55, 66}; int n; /* Calculating the size of the array with this formula. * n = sizeof(array_name) / sizeof(array_name[0]) * This is a universal formula to find number of elements in * an array, which means it will work for arrays of all data * types such as int, char, float etc.
🌐
Cplusplus
cplusplus.com › forum › beginner › 3699
calc. number of elements in array - C++ Forum
August 22, 2008 - However, std::vector<int> should be preferred. It has a size()-function returning the number of elements. As soon as array to pointer decay ocurres (which will happen really fast), your method won't work anymore. ... i tried this, but sizeof(array) always comes out to be 4, being an integer ...
🌐
w3tutorials
w3tutorials.net › blog › how-do-i-determine-the-size-of-my-array-in-c
How to Determine the Size of an Array in C: Number of Elements Explained — w3tutorials.net
For a static array (size known at compile time), sizeof(array) returns the total number of bytes occupied by the array. To get the number of elements, divide this by the size of one element (sizeof(array[0])).
🌐
DigitalOcean
digitalocean.com › community › tutorials › find-array-length-in-c-plus-plus
How to Find the Length of an Array in C++ | DigitalOcean
April 17, 2025 - The size or length of the array here is equal to the total number of elements in it - which is 5. There are a few methods through which we can determine the length of an array in C++ language.
🌐
w3resource
w3resource.com › c-programming-exercises › array › c-array-exercise-5.php
C : Count the total number of duplicate elements in an array
September 27, 2025 - Input the number of elements to be stored in the array :5 Input 5 elements in the array : element - 0 : 1 element - 1 : 1 element - 2 : 2 element - 3 : 3 element - 4 : 3 Total number of duplicate elements found in the array: 2 ... Write a C program to count the number of duplicate elements in an array without using additional arrays.
🌐
w3resource
w3resource.com › c-programming-exercises › array › c-array-exercise-8.php
C Program: Count the frequency of each element of an array - w3resource
September 27, 2025 - Count frequency of each element of an array: ------------------------------------------------ Input the number of elements to be stored in the array :3 Input 3 elements in the array : element - 0 : 25 element - 1 : 12 element - 2 : 43 The frequency of all elements of array : 25 occurs 1 times 12 occurs 1 times 43 occurs 1 times
🌐
w3resource
w3resource.com › c-programming-exercises › array › c-array-exercise-2.php
C : Read n values of an array, display them in reverse order
Read n number of values in an array and display it in reverse order: ------------------------------------------------------------------------ Input the number of elements to store in the array :3 Input 3 number of elements in the array : element - 0 : 2 element - 1 : 5 element - 2 : 7 The values store into the array are : 2 5 7 The values store into the array in reverse are : 7 5 2
🌐
Sololearn
sololearn.com › en › Discuss › 3103668 › how-to-find-how-many-elements-are-filled-in-an-array
How to find how many elements are filled in an array | Sololearn: Learn to code for FREE!
November 13, 2022 - #include <stdio.h> #include <conio.h> #include <stdlib.h> #define size 50 int arr[size], choice, top = 0, position, insert, fixedtop, i, j = 1; int insertatspecpos(); // Inserting value at specific position int traversearr(); // Printing all the values of an array int deletearr(); // not completed int insertatFpos(); // Inserting value at last position int insertatLpos(); // Inserting value at first position int main() { while (1) { printf("\n"); printf("\n 1) Insert element in an array \n 2) Insert at specific location \n 3) Traversal of an array \n 4) Insert at first location \n 5) Deletion of an array \n 6) Exit from a program"); printf("\n\nSelect the no to perform an operations: \n"); scanf("%d", &choice); if (choice == 1) { insertatLpos(); } else if (choice == 2) { insertatspecpos(); } else if (choice == 3) { traversearr(); } else
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 188400 › how-to-count-the-elements-of-an-array
c++ - How to count the elements of an array? | DaniWeb
Or you could use a counter to keep track of the number of ints. Or you could use a vector<int> Could you tell us why you need this functionality? ... To the OP: You cannot !. Yes you cannot. However surprise it may bring to you, the truth is you really you cannot. Thats why most of functions dealing with int array often asks you to enter the length of the array as one of the argument.
Top answer
1 of 16
1753

Executive summary:

int a[17];
size_t n = sizeof(a)/sizeof(a[0]);

Full answer:

To determine the size of your array in bytes, you can use the sizeof operator:

int a[17];
size_t n = sizeof(a);

On my computer, ints are 4 bytes long, so n is 68.

To determine the number of elements in the array, we can divide the total size of the array by the size of the array element. You could do this with the type, like this:

int a[17];
size_t n = sizeof(a) / sizeof(int);

and get the proper answer (68 / 4 = 17), but if the type of a changed you would have a nasty bug if you forgot to change the sizeof(int) as well.

So the preferred divisor is sizeof(a[0]) or the equivalent sizeof(*a), the size of the first element of the array.

int a[17];
size_t n = sizeof(a) / sizeof(a[0]);

Another advantage is that you can now easily parameterize the array name in a macro and get:

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int a[17];
size_t n = NELEMS(a);
2 of 16
1120

The sizeof way is the right way iff you are dealing with arrays not received as parameters. An array sent as a parameter to a function is treated as a pointer, so sizeof will return the pointer's size, instead of the array's.

Thus, inside functions this method does not work. Instead, always pass an additional parameter size_t size indicating the number of elements in the array.

Test:

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

void printSizeOf(int intArray[]);
void printLength(int intArray[]);

int main(int argc, char* argv[])
{
    int array[] = { 0, 1, 2, 3, 4, 5, 6 };

    printf("sizeof of array: %d\n", (int) sizeof(array));
    printSizeOf(array);

    printf("Length of array: %d\n", (int)( sizeof(array) / sizeof(array[0]) ));
    printLength(array);
}

void printSizeOf(int intArray[])
{
    printf("sizeof of parameter: %d\n", (int) sizeof(intArray));
}

void printLength(int intArray[])
{
    printf("Length of parameter: %d\n", (int)( sizeof(intArray) / sizeof(intArray[0]) ));
}

Output (in a 64-bit Linux OS):

sizeof of array: 28
sizeof of parameter: 8
Length of array: 7
Length of parameter: 2

Output (in a 32-bit windows OS):

sizeof of array: 28
sizeof of parameter: 4
Length of array: 7
Length of parameter: 1