C String Length

C String Length


The syntax of strlen() function is

size strlen(const char *str);


  • size is an integer variable which is the length of the string str.
  • strlen will give the length of the string until \0 character encountered.

Note#1 : strlen(“”) differs from strlen(NULL)n=strlen(“”) , “” is a string containing null character.so,it returns the size as 0.n=strlen(NULL) gives undefined behavior because null is a constant and its not a string.

Note#2 : What happens to String Length if String is not terminated with null?UndefinedBehavior. printf will interpret “%s” as a standard C string. This means that the code that is generated will simply keep reading characters until it finds a null terminator (\0). This may eventually cross a memory boundary and cause an error or it may produce no output or crash or so on.