How to use strlen() in C

Last Updated On Wednesday 21st Jul 2021
  • The strlen function returns the number of characters inside a null-terminated character array, excluding the null-terminating character.

strlen c

	sizet_t strlen (const char* str);
	

Example 1

  • To use this function, we include the <string.h> header.
	#include <stdio.h>
#include <string.h>

int main(void) {
  const char str[] = "Find this String Length";
  size_t strLength = strlen(str);
  printf("The string contains %zu characters.\n", strLength);
}
	

The Above Code Outputs the

	// The string contains 23 characters.
	

Example 2

Using const char *p pointer to a character string.

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

int main(void) {
  const char * p = "Clap along if you feel like happiness is the truth";
  size_t strLength = strlen(p);
  printf("The string contains %zu characters.\n", strLength);
}
	

The Above Code Outputs the

	// The string contains 50 characters.