Javatpoint Logo
Javatpoint Logo

std::stol function in C++

In this article, you will learn about the std::stol function in C++ with its syntax and examples.

What is std::stol()?

The Standard Template Library (STL) in C++ includes the std::stol() function, which is specifically made for converting strings to long integers. This function is especially helpful when working with user input or reading data from external sources-where the input is usually given in string format. It returns the equivalent long integer after receiving a string as input. An exception of type std::invalid_argument or std::out_of_range is thrown if the conversion is not possible or if the input string contains characters that are not part of a valid number.


It has the following syntax:

long std::stol(const std::string& str, size_t* pos = 0, int base = 10);

str: The text that needs to be changed into a long integer.

Pos: The index of the string's first unconverted character, stored as a pointer to a size_t object. If it is not required, this optional parameter can be set to nullptr.

base: The conversion's numerical base. Ten is the default.

Return Value: The numeric value that was parsed from the input string is returned by the function as a long integer.

Exceptions: The function throws an exception if the conversion cannot be completed or if characters in the input string are invalid. If no conversion could be done, the possible exceptions are std::invalid_argument and std::out_of_range if the converted value is greater than the representable range for a long.

Use Cases:

  • Input Processing by Users:

std::stol() is useful for transforming numerical input that is received from users in string format into a numeric type so that it may be processed further.

  • Input/Output File:

Reading numerical data in files containing strings as the data's representation.

  • Data validation:

Data validation involves confirming that user inputs accurately represent long numbers by validating and verifying them.

Example Program 1:

Let's take an example to illustrate the use of std::stol function in C++.


std::stol function in C++


  1. Intialization:
    • A string variable called numStr is initialized with the value "12345" at the beginning of the programme.
  2. Try Block:
    • The code moves into a try block, signalling that it will try to run the statements contained within. Potential exceptions are dealt with here.
  3. Conversion Attempt:
    • The program tries to use std::stol() to transform the string numStr into a long integer inside the try block.
  4. Effective Conversion:
    • The outcome is kept in the variable result if the conversion is successful.
  5. Output Converted Value:
    • The successfully converted number is then printed to the standard output by the program.
  6. Treating Exceptions:
    • One of the catch blocks handles any exceptions that arise during the conversion.
  7. Exception for Catching Invalid Argument:
    • An error message stating that the argument is invalid is printed if the exception is of the type std::invalid_argument.
  8. Exception for Catch Out of Range:
    • An error message stating that the conversion result is outside of the valid range is printed if the exception is of the type std::out_of_range.
  9. Return Policy:
    • The program returns 0 to the operating system, signifying successful execution, and the main function ends.

In brief, the code tries to use the std::stol() function inside of a try block to convert the string "12345" to a long integer. The result is printed if the conversion is successful. It captures exceptions and prints the relevant error message in the event that one arises (either because of an incorrect argument or because the result is outside of range).

Finally, the program returns 0, signifying successful completion.

Example Program 2:

Let's take another example to illustrate the use of std::stol function in C++.


std::stol function in C++

Explanation :

  • In this example, the user is prompted to enter string values to be converted.
  • The application will keep accepting user input until the user inputs "exit" using a while loop.
  • Std::cin is used inside the loop to read user input.
  • Entering "exit" causes this program to print a farewell message and end the loop.
  • If not, it tries to use std::stol to convert the input to a long.
  • The converted value is printed if the conversion is successful.
  • It catches exceptions (invalid arguments, out of range), prints an error message, and continues.
  • As it waits for new user input, the loop moves on to the next iteration.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA