std::string::size() in C++ Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string.In this article, we will learn about string::size() method in C++.Syntaxs.size()where s is the string whose length is to be found.ParametersThis function does not take any parameters.Return ValueReturns the size of the string as an unsigned integer size_t.If the string is empty, it returns 0.Example of string::size() C++ // C++ Program to demonstrate how to use string::size() // to get the number of characters in a string #include <bits/stdc++.h> using namespace std; int main() { string s1 = "abcd"; string s2 = ""; string s3 = "#&&"; // Getting the size of the strings cout << "String 1 Size: " << s1.size() << endl; cout << "String 2 Size: " << s2.size() << endl; cout << "String 3 Size: " << s3.size(); return 0; } OutputString 1 Size: 4 String 2 Size: 0 String 3 Size: 3Time Complexity: O(1)Auxiliary Space: O(1)Note: string::size() function only works with C++ Style strings i.e. std::string objects. For C-Style strings i.e. array of characters, use strlen() function.Difference between string::size() and string::length()There is no difference between the functions string::size() and string::length(). They both do the same task, have similar signature and can be used interchangeably. They are just there for consistency and people's preferences. Comment More info A abhishekcpp Follow Improve Article Tags : C++ cpp-string CPP-Functions cpp-strings-library Explore C++ BasicsIntroduction to C++3 min readData Types in C++7 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++10 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library2 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like