Get Length of Char Array in C

  1. Use the sizeof Operator to Find Length of Char Array
  2. Use the strlen Function to Find Length of Char Array

This article will explain several methods of getting the length of a char array in C.

Use the sizeof Operator to Find Length of Char Array

Array size can be calculated using sizeof operator regardless of the element data type. Although, when measuring the array’s size, there might be some hideous errors if the inner details are ignored.

Namely, the following example is initializing two arrays named arr and arr2 with different notations. Then sizes of both arrays are retrieved using the sizeof operator and printed to the console.

Note that the second array size is equal to 18 bytes even though there are only 17 printed elements. The cause of this issue is hiding in the method of initialization, namely when char array is initialized using a string literal value, the terminating null byte is also stored as a part of the array. Thus the sizeof operator includes this byte in the sum of all elements and returns the corresponding result.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void printCharArray(char *arr, size_t len)
{
    for (size_t i = 0; i < len; ++i) {
        printf("%c, ", arr[i]);
    }
    printf("\n");
}

int main(int argc, char *argv[]){
    char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
    char arr2[] = "array initialized";

    printCharArray(arr, sizeof arr);
    printf("size = %lu bytes \n", sizeof arr);

    printf("\n");

    printCharArray(arr2, sizeof arr2-1);
    printf("size = %lu bytes \n", sizeof arr2);

    exit(EXIT_SUCCESS);
}

Output:

a, b, c, d, e, f, g,
size = 7 bytes
size = 24 bytes

a, r, r, a, y,  , i, n, i, t, i, a, l, i, z, e, d,
size = 18 bytes
size = 17 bytes

Use the strlen Function to Find Length of Char Array

In some scenarios, char arrays that were initialized or stored as the null-terminated character strings can be measured for size using the strlen function, which is part of the C standard library string utilities.

If we run the previous example code with strlen calculated values, we get different numbers caused by separate issues. The first - arr object size is printed to be 24 bytes because the strlen function iterates through a char array until the terminating null byte is encountered. Thus, calling strlen on arr object resulted in iterating over both arr and arr2, since the first array is not terminated with null byte and the compiler stored the arr2 continuously after it, resulting in a size equal to the sum of both arrays minus 1 (discarding the terminating null byte).

Note that, lengthOfArray function implemented by us imitates the strlen counting behavior and results in the same numbers. For the second array object - arr2, strlen returned the number of printed characters without the last null byte, which can be useful in some scenarios, but it does not represent the array’s actual size.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void printCharArray(char *arr, size_t len)
{
    for (size_t i = 0; i < len; ++i) {
        printf("%c, ", arr[i]);
    }
    printf("\n");
}

long lengthOfArray(const char *arr)
{
    long size = 0;

    while (*arr) {
        size += 1;
        arr +=1;
    }

    return size;
}

int main(int argc, char *argv[]){
    char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
    char arr2[] = "array initialized";

    printCharArray(arr, sizeof arr);
    printf("size = %lu bytes \n", sizeof arr);
    printf("size = %lu bytes \n", strlen(arr));
    printf("size = %lu bytes \n", lengthOfArray(arr));

    printf("\n");

    printCharArray(arr2, sizeof arr2-1);
    printf("size = %lu bytes \n", sizeof arr2);
    printf("size = %lu bytes \n", strlen(arr2));
    printf("size = %lu bytes \n", lengthOfArray(arr2));

    exit(EXIT_SUCCESS);
}

Output:

a, b, c, d, e, f, g,
size = 7 bytes
size = 24 bytes
size = 24 bytes

a, r, r, a, y,  , i, n, i, t, i, a, l, i, z, e, d,
size = 18 bytes
size = 17 bytes
size = 17 bytes
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C Char

  • Get a Substring in C