Thursday, January 30, 2025
HomeProgrammingHow Do I Determine the Size of My Array in C?

How Do I Determine the Size of My Array in C?

In C, you can determine the size of an array by using the sizeof operator. To calculate the number of elements in a statically declared array, you divide the total size of the array by the size of one element. Here’s an example:

c
#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
size_t size = sizeof(arr) / sizeof(arr[0]);
printf("Array size: %zu\n", size);
return 0;
}

In this code, sizeof(arr) gives the total byte size of the array, while sizeof(arr[0]) gives the size of one element. The result is the number of elements in the array.

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