Sunday, January 19, 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 sizeof operator, which gives the memory size in bytes. Divide the total size of the array by the size of an individual element:

Example:

c
int arr[] = {1, 2, 3, 4, 5};
size_t size = sizeof(arr) / sizeof(arr[0]);
printf("The size of the array is: %zu\n", size);

Explanation:

  • sizeof(arr) gives the total memory size of the array.
  • sizeof(arr[0]) gives the size of one element.
  • The division yields the number of elements in the array.
See also  How can a frequency table be created in R?

Note: This works only for arrays declared in the same scope, not pointers.

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