C++ std::stoll() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::stoll

Definition

In C++, std::stoll() converts a string to long long.

Include <string>.

This is a free function from <string> that works with std::string (and sometimes wstring). Member functions like find live on the string object with a dot; helpers like this are called as std::stoll(...).

Syntax

Pass the text to parse.

C++
long long n = std::stoll("9000000000");
CallWhat it does
std::stoll(str)Parse as long long.

Good for big IDs that still fit in 64-bit signed range.

Library declarations
C++
long long stoll(const string& str, size_t* idx = 0, int base = 10);

Parameters

ParameterDescription
strInput.
idx, baseOptional.

Return Value

Return ValueDescription
long longParsed value.

C++ Examples

Example 1 — Basic stoll()

Parse a large value.

C++
#include <iostream>
#include <string>

int main() {
    long long n = std::stoll("9000000000");
    std::cout << n << '\\n';
    return 0;
}

How It Works

  1. The number is larger than a typical 32-bit int max.
  2. stoll stores it in long long.
  3. Output shows the full value.

Example 2 — stoll With idx

Parse prefix of mixed text.

C++
#include <iostream>
#include <string>

int main() {
    std::size_t i = 0;
    long long n = std::stoll("77xyz", &i);
    std::cout << n << ' ' << i << '\\n';
    return 0;
}

How It Works

  1. Only 77 is numeric prefix.
  2. idx marks the 'x'.
  3. Values printed are 77 and 2.

Example 3 — stoll Invalid

Handle invalid_argument.

C++
#include <iostream>
#include <string>

int main() {
    try {
        std::stoll("?");
    } catch (const std::invalid_argument&) {
        std::cout << "fail\\n";
    }
    return 0;
}

How It Works

  1. No number at the start.
  2. Exception is thrown.
  3. Message printed.

Frequently Asked Questions

String to long long conversion.
<string>.
stoull.
Yes on invalid/out of range.
Same 2–36 support as stoi.

Next: stoul()

Keep going through the string functions. Convert a string to unsigned long.

stoul() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful