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 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.

Example:

    Input:
    str = "123";

    Function call:
    atol(str);

    Output:
    123

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.