Sunday, January 12, 2025
HomeProgrammingHow do I determine the size of my array in C?

How do I determine the size of my array in C?

To determine the size of an array in C, use the formula:

sizeof(array) / sizeof(array[0]);

sizeof(array) gives the total memory size of the array, and sizeof(array[0]) gives the size of a single element. Dividing these provides the number of elements in the array. For example:

int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Array size: %d", size);

This works only for statically declared arrays, not for pointers or dynamically allocated arrays, as sizeof on a pointer only gives the size of the pointer, not the full array.

See also  Difference between Left Join and Right Join
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x