strlen() in C++
Overview
The strlen() function returns the length of the given string, excluding the null character('\0'). It is defined in the cstring header file in C++.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Introduction
The strlen() function returns the length of the given string, excluding the null character('\0'). It is defined in the cstring header file in C++. The strlen() in c++ shows undefined behaviour when the string passed to it is not null terminated.
Syntax of strlen() in C++
str is the name of the string whose length we need to find, and it is typecast to const char*.
Parameters of strlen() in C++
strlen() in c++ accepts a pointer to the null-terminated string whose length is to be calculated.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Return Value of strlen() in C++
The strlen() in c++ returns the length of the string passed.
Note: The returned length does not include the null character '\0'.
Example of strlen() in C++
This program demonstrates the use of strlen() function.
Output :
Turn Learning into Career Growth
strlen() Prototype
The prototype of the strlen() in c++ is defined in cstring header file :
The size_t is used to represent size of variables/objects in bytes.
- It guarantees to be big enough to contain the size of the biggest object that the system can handle.
- If a system is 32 bits, then size_t represents unsigned int.
- If a system is 64 bits, then size_t represents unsigned long long.
Difference between size_t and int is that in case of size_t, it is not defined how large it is, i.e., it can be long int or long long int but int size is fixed.
strlen() Undefined Behavior
The strlen() in c++ shows undefined behaviour when the string passed to it is not null terminated.
Example showing undefined behavior:
Output:
Explanation: As we know that the undefined behavior of strlen() is that, it is meant to be used with char arrays or null terminated strings, so when it is used with a string which is not null-terminated, either it produces an unexpected result or it fails to produce a result.
More Examples of strlen() in C++?
1. Program to show the mechanism of the strlen() function.
Output :
Explanation : This is the usual behaviour of strlen() function in C++, as discussed before, and it will return the length of the string passed.
2. Program to check if the length of two strings is equal or not using the strlen() function.
Output:
Explanation : The length of the two strings, a and b are equal, i.e, 4, Therefore, it prints string lengths are equal.
Conclusion
- The strlen() function returns the length of the given string excluding the null character('\0').
- strlen() function is defined in the cstring header file in C++.
- strlen() in C++ shows undefined behavior when the string passed to it is not null terminated, therefore, it is recommended to include null character at the end of the string when declared as character array.




