strlen() in C++

Video Tutorial
FREE
String Length thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

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++.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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*.

Sharpen Your Fundamentals with Free Learning

Parameters of strlen() in C++

strlen() in c++ accepts a pointer to the null-terminated string whose length is to be calculated.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

  1. The strlen() function returns the length of the given string excluding the null character('\0').
  2. strlen() function is defined in the cstring header file in C++.
  3. 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.