×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

atol() Function with Example in C++

C++ atol() function: Here, we are going to learn about the atol() function with example of cstdlib header in C++ programming language.
Submitted by IncludeHelp, on May 26, 2020

C++ atol() function

atol() function is a library function of cstdlib header. It is used to convert the given string value to the integer value. It accepts a string containing an integer (integral) number and returns its long integer value.

Syntax

Syntax of atol() function:

C++11:

long int atol (const char * str);

Parameter(s)

  • str – represents a string containing an integer (integral) number.

Return value

The return type of this function is long int, it returns the long integer converted value.

Sample Input and Output

Input:
str = "123";

Function call:
atol(str);

Output:
123

Example

C++ code to demonstrate the example of atol() function:

// C++ code to demonstrate the example of
// atol() function

#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std;

// main() section
int main()
{
    char str[50];

    strcpy(str, "123");
    cout << "atol(\"" << str << "\"): " << atol(str) << endl;

    strcpy(str, "-123");
    cout << "atol(\"" << str << "\"): " << atol(str) << endl;

    strcpy(str, "0");
    cout << "atol(\"" << str << "\"): " << atol(str) << endl;

    strcpy(str, "1234567");
    cout << "atol(\"" << str << "\"): " << atol(str) << endl;

    strcpy(str, "12345678");
    cout << "atol(\"" << str << "\"): " << atol(str) << endl;

    strcpy(str, "-12345678");
    cout << "atol(\"" << str << "\"): " << atol(str) << endl;

    return 0;
}

Output

atol("123"): 123
atol("-123"): -123
atol("0"): 0
atol("1234567"): 1234567
atol("12345678"): 12345678
atol("-12345678"): -12345678

Reference: C++ atol() function



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.