This function calculates the length of the string (not including the null terminator).
Syntax:
strlen(stringName);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "My World";
printf("Length of string: %d", strlen(str)); // Output: 8
return 0;
}
Output:
8
Note: string.h is a standard header file which is use to string in the function.
C strlen() method – Interview Questions
Q 1: What does strlen() do?
Ans: It returns the length of a string excluding the null character.
Q 2: Which header file contains strlen()?
Ans: .
Q 3: What is the return type of strlen()?
Ans: size_t.
Q 4: Does strlen() count spaces?
Ans: Yes, spaces are counted as characters.
Q 5: What happens if the string has no null terminator?
Ans: It results in undefined behavior.
C strlen() method – Objective Questions (MCQs)
Q1. What does the strlen() function do in C?
Q2. Which header file is required to use strlen()?
Q3. What will strlen("Hello") return?
Q4. Does strlen() count the null character \0?
Q5. What is the return type of strlen()?