strol() function in C++ Last Updated : 21 Nov, 2021 Comments Improve Suggest changes Like Article Like Report The strtol() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long int.This function also sets an end pointer that points to the first character after the last valid numeric character of the string, if there is no such character then the pointer is set to null. This function is defined in cstdlib header file.Syntax: long int strtol(const char* str, char** end, int base) Parameter :The function accepts three mandatory parameters which are described below. str: A string consist of an integral number.end: This is reference to object of type char*. The value of end is set by the function to the next character in str after the last valid numeric character. This parameter can also be a null pointer, in case if it is not used.base: It represent the numerical base (radix) that determines the valid characters and their interpretation in the string Return type :The function returns two types of values which are described below: If valid conversion occur then the function returns the converted integral number as long int value.If no valid conversion could be performed, a zero value is returned. Below programs illustrate the above function. Program 1: CPP // C++ program to illustrate the // strtol() function #include <bits/stdc++.h> using namespace std; // Driver code int main() { int base = 10; char str[] = "123abc"; char* end; long int num; // Function used to convert string num = strtol(str, &end, base); cout << "Given String = " << str << endl; cout << "Number with base 10 in string " << num << endl; cout << "End String points to " << end << endl << endl; // in this case the end pointer points to null strcpy(str, "12345"); // prints the current string cout << "Given String = " << str << endl; // function used num = strtol(str, &end, base); // prints the converted integer cout << "Number with base 10 in string " << num << endl; if (*end) { cout << end; } else { cout << "Null pointer"; } return 0; } Output: Given String = 123abc Number with base 10 in string 123 End String points to abc Given String = 12345 Number with base 10 in string 12345 Null pointer Program 2: CPP // C++ program to illustrate the // strtol() function Program to // convert multiple values at different base #include <bits/stdc++.h> using namespace std; // Driver code int main() { char str[] = "100 ab 123 1010"; char* end; long int a, b, c, d; // base 10 a = strtol(str, &end, 10); // base 16 b = strtol(end, &end, 16); // base 8 c = strtol(end, &end, 8); // base 2 d = strtol(end, &end, 2); cout << "The decimal equivalents of all numbers are \n"; cout << a << endl << b << endl << c << endl << d; return 0; } Output: The decimal equivalents of all numbers are 100 171 83 10 Comment More infoAdvertise with us Next Article strol() function in C++ A Aman Goyal 2 Follow Improve Article Tags : Misc C++ STL CPP-Functions Practice Tags : CPPMiscSTL Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie 7 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br 14 min read Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan 14 min read Waterfall Model - Software Engineering The Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because 13 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s 6 min read Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec 14 min read Like