To get the length of an array in C, you can divide the size of the entire array by the size of one element.
In this example,
numbers
with some values.length
variable.length
to standard output.#include <stdio.h>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int length = sizeof(numbers) / sizeof(numbers[0]);
printf("Length of array is: %d", length);
return 0;
}
Length of array is: 5
In this example,
letters
with some characters.length
variable.length
to standard output.#include <stdio.h>
int main() {
char letters[] = {'a', 'b', 'c', 'd', 'e'};
int length = sizeof(letters) / sizeof(letters[0]);
printf("Length of array is: %d", length);
return 0;
}
Length of array is: 5
In this tutorial, we learned How to Get Array Length in C language with well detailed examples.