How to check how many elements an array has?

In C it's possible to omit typing array size if it's fields are provided immediately, so the compiler can easily calculate the size by itself. This is presented below, note empty [] parenthesis.

    char* sampleArray[] = {
        "Hello world!",
        "How are you?",
        "You like coffe?"
    };

We can also specify the size directly:

    char* sampleArray[2] = {
        "Hello world!",
        "How are you?",
        "You like coffe?"
    };

Or use the best way:

    const int SampleArraySize = 2;

    char* sampleArray[SampleArraySize] = {
        "Hello world!",
        "How are you?",
        "You like coffe?"
    };

In the first two examples we don't have a variable that would tell us how many elements the array has. Sure, we may hardcode it, but it's not a good idea from a maintainability point of view.

We may calculate the size of above array by using a simple trick:

int sampleArrayLength = sizeof(sampleArray) / sizeof(sampleArray[0]);

Note that it will work with all built in types and if the size or even the type of an array will be changed in the future, this line will still be correct.

Below you may see a simple use case.

#include <stdio.h>


int main()
{
    char* sampleArray[] = {
        "Hello world!",
        "How are you?",
        "You like coffe?"
    };

    int sampleArrayLength = sizeof(sampleArray) / sizeof(sampleArray[0]);

    printf("someStrings array has %d  elments counting from one.\n", sampleArrayLength);

    for(int i = 0U; i < sampleArrayLength; i++)
    {
        printf("#%d element: %s\n", i, sampleArray[i]);
        
    }
    
    return 0;
}
sh-4.3$ main                                                                                                                                                                                                                                            
someStrings array has 3  elments counting from one.                                                                                                                                                                                                     
#0 element: Hello world!                                                                                                                                                                                                                                
#1 element: How are you?                                                                                                                                                                                                                                
#2 element: You like coffe?

Unfortunately, it will not work for arrays passed as pointers - for them, we need to pass size of an array as an additional argument.

How to check how many elements an enum has?

While C language doesn't offer build in method to check how many elements an enum has, it's still possible to obtain this information by using a simple trick.

The idea is to add a dummy element at the very end, since numeric enums values are 0, 1, 2, 3, 4, 5, ..., the numeric value of last element will be also the amount of elements that this enum has.

#include <stdio.h>

typedef enum Fruit {
    FRUIT_APPLE,
    FRUIT_ORANGE,
    FRUIT_BLACKBERRY,
    /* place new elements below */
    
    /* guard */
    FRUIT_LAST_ELEMENT
} Fruit;

int main()
{
    int fruitEnumLength = (FRUIT_LAST_ELEMENT - 1);
    printf("Fruit enum has %d elments counting from zero.\n", fruitEnumLength);

    return 0;
}
sh-4.3$ gcc -o main *.c                                                                                                                                                                                                                                 
sh-4.3$ main                                                                                                                                                                                                                                            
Fruit enum has 2 elments counting from zero.  

This technique may be useful for sanitizing input data or for writing tests.