Facebook Page Twitter Page LinkedIn Page
× C++ Tutorial C++ Advanced C++ Resources

The C++ string::max_size function is used to return the maximum length the string can reach. The function returns the maximum potential length the string can reach due to known system or library implementation limitations.


size_type max_size() const;
size_type max_size() const noexcept;


No parameter is required.

Return Value

Maximum number of characters that can be held in a string.

Time Complexity

Constant i.e, Θ(1)


In the below example, the string::max_size function is used find out the maximum number of characters that a string can hold.

#include <iostream>
using namespace std;
int main (){
  string MyString = "Hello World!.";

  cout<<"Size of the String: "<<MyString.size()<<"\n";
  cout<<"Length of the String: "<<MyString.length()<<"\n";
  cout<<"Capacity of the String: "<<MyString.capacity()<<"\n";
  cout<<"Maximum size of the String: "<<MyString.max_size()<<"\n"; 
  return 0;

A possible output could be:

Size of the String: 13
Length of the String: 13
Capacity of the String: 15
Maximum size of the String: 9223372036854775807

❮ C++ - String Functions