In C, you can use the sizeof
operator to check if an array is not empty.
In this example,
numbers
with some elements.sizeof
and dividing it by the size of an array element, and store the result in the variable notEmpty
.#include <stdio.h>
int main() {
int numbers[] = {1, 2, 3};
int notEmpty = sizeof(numbers) / sizeof(numbers[0]);
printf("Array is not empty: %s\n", notEmpty ? "true" : "false");
return 0;
}
Array is not empty: true
In this example,
emptyArray
.sizeof
and dividing it by the size of an array element, and store the result in the variable notEmpty
.#include <stdio.h>
int main() {
int emptyArray[] = {};
int notEmpty = sizeof(emptyArray) / sizeof(emptyArray[0]);
printf("Array is not empty: %s\n", notEmpty ? "true" : "false");
return 0;
}
Array is not empty: false
In this tutorial, we learned How to check if an Array is Not Empty in C language with well detailed examples.