strlen function-get the length of a string

Use the strlen function to get the length of the string. You can use the strlen function by reading string.h.

#include <string.h>
size_t strlen (const char * s);

In C language, there is a promise that strings end with "\ 0". The strlen function assumes this convention and calculates the length of the string. In other words, it loops and counts the number of characters until "\ 0" is found. Conversely, if the string does not end with "\ 0", it will go to an unintended memory area and a buffer overrun will occur.

When using strlen, make sure that you are using it for strings ending in "\ 0".

This is a sample to find the length of a character string with the strlen function.

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

int main (void) {
  const char * string = "Hello";
  
  int32_t string_length = strlen (string);
  
  printf("%d\n", string_length);
}

This is the output result.

Five

Associated Information