HOWTO · C

How to Get Length of Char Array in C

This article demonstrates how to get the length of a char array in C.

On this page

In C, “length” can mean the number of elements reserved in a char array or the number of characters before a string’s null terminator. Use sizeof for an array’s storage and element count while the array type is still available. Use strlen only for a valid null-terminated string.

How to Get the Length of a Char Array in C Using the sizeof Operator

sizeof array gives the array’s size in C bytes. Divide it by sizeof array[0] to get the number of elements. For a char array, each element occupies one C byte; a C byte is not necessarily an eight-bit octet.

Run this example:

#include <stdio.h>

int main(void) {
  char letters[] = {'C', 'o', 'd', 'e'};
  char word[] = "Code";
  size_t letters_count = sizeof letters / sizeof letters[0];
  size_t word_capacity = sizeof word / sizeof word[0];
  size_t word_length = word_capacity - 1;

  printf("letters: %zu elements, %zu bytes\n", letters_count, sizeof letters);
  printf("word: %zu characters, %zu elements including '\\0'\n",
         word_length, word_capacity);
  return 0;
}
letters: 4 elements, 4 bytes
word: 4 characters, 5 elements including '\0'

The letters initializer reserves four elements and has no terminator. The string literal initializes word with five elements: four visible characters and the terminating \0. Subtract one only when the array is known to contain that terminator and the desired value is the string length. %zu is the matching printf conversion for size_t.

sizeof measures the whole array only where the array type is available. In a function parameter such as char text[], the parameter is adjusted to a pointer; sizeof text then measures the pointer. Pass the element count separately when a function needs the array length. See size_t for the unsigned type used for sizes.

How to Get the Length of a Char Array in C Using the strlen Function

strlen, declared in <string.h>, counts characters up to but not including the first \0. Its argument must point to a valid null-terminated string. It does not know the array’s capacity and cannot safely find the end of an array that lacks a terminator. See null-terminated strings for how that representation works.

For example, strlen("Code") returns four: the literal has five array elements, but the terminator is not counted. The call is suitable because the literal supplies the terminator. Do not call strlen on an explicitly sized array unless you know a terminator occurs within the accessible array.

How to Get the Length of a Char Array in C by Manual Counting

Manual counting is useful when you have a bound or a valid terminator. For an array with a known element count count, visit indices from 0 through count - 1; do not read at array[count]. If the goal is the length of a string stored in that array, stop at \0 and ensure the scan never passes the array’s bound.

This example uses the array’s element count as a bound and also stops at its terminator. Because text is initialized from a string literal, its terminator is within the array. The loop checks the bound before reading each element, so it never accesses text[sizeof text]. The example prints 4 for the four characters in Code.

#include <stdio.h>

int main(void) {
  char text[] = "Code";
  size_t length = 0;
  size_t capacity = sizeof text / sizeof text[0];

  while (length < capacity && text[length] != '\0') {
    ++length;
  }

  printf("%zu\n", length);
  return 0;
}

For unterminated data, use the separately supplied element count and process only those elements; do not scan until \0 unless a terminator is guaranteed within the bound.

How to Get the Length of a Char Array in C Using Pointers

A pointer can traverse a string, but it does not carry the array’s capacity. For a valid null-terminated string, traversal can advance while *ptr != '\0'; this has the same terminator requirement as strlen. This example takes the address of an array created from a string literal, so a terminator is guaranteed within the known bound. It checks the end pointer before dereferencing and also stops at the terminator. The example prints 4 for the four characters in Code.

#include <stdio.h>

int main(void) {
  char text[] = "Code";
  char *ptr = text;
  char *end = text + sizeof text / sizeof text[0];

  while (ptr < end && *ptr != '\0') {
    ++ptr;
  }

  printf("%zu\n", (size_t)(ptr - text));
  return 0;
}

For data without a terminator, keep a separate count or end pointer and stop at that known bound. When a function receives a pointer to the first element, pass the element count as another argument if it must traverse an array whose length is not represented by a string terminator. Pointer arithmetic alone cannot determine where an array ends. See C array length for the related array-size distinction.

Conclusion

Choose the measurement that matches the task: sizeof array / sizeof array[0] gives an array’s element count where its full type is available; subtract the terminator only for a known terminated array when seeking its string length. strlen counts before \0 in a valid null-terminated string. Manual and pointer traversal need a valid terminator or an explicit bound. Keep array lengths available when passing arrays to functions, because those parameters are pointers.