C++ cstdlib aoti , atol , atoll


The C++ <cstdlib> atoi , atol and atoll converts the ‘const char*‘ passed to the function to the integer type.They are same with the functions found in C <stdlib.h> header.The declaration of the functions are given below

1 int atoi(const char *x);
2 long int atol(const char *x);
3 long long int atoll(const char *x); (C++11)

Parameters:
x -A pointer to const char* type or a string literal.

Return type
integer type – atoi returns ‘int’, atol returns ‘long int’ and atoll returns ‘long long’ .

Some points to note:

i) If the passed argument is a combination of number and non-number characters with the number occurring at the initial part,then only the number is converted to int ,long int or long long int type.The non-number characters part is neglected.

ii) If the argument passed is only non-number characters or the non-number character occur at first then 0 is returned.

iii) If the the string literal passed consists of a point(“.”) in between any of the number characters the characters after the point is neglected.

iv) If the function cannot represent the number after converting from the string parameter,then only the bits within the size of the return type is taken and the integer value obtain from converting the bits using the base-2 numeral method is returned.

Code example

cout<< “atoi( \”23.45\” )=” << atoi( “23.45” ) << endl
<< “atol( \”2580String\” )=” << atol( “2580String” ) << endl
<< “atoll( \”23.9e5\” )=” << atoll( “23.9e5” ) << endl ;

/*Passing characters non-convertible to integer type */
cout<< “atol( \”F3F33\” )=” << atol( “F3F33” ) << endl ;

/*Checking the return type of the three fucntions */
cout<< “typeid( atoi( \”33\” ) ).name( )=” << typeid( atoi( “33” ) ).name( ) << endl
<< “typeid( atol( \”3489\” ) ).name( )=” << typeid( atol( “3489” ) ).name( ) << endl
<< “typeid( atoi( \”0023\” ) ).name( )=” << typeid( atoll( “0023” ) ).name( ) << “\n\n”;

/*A case when the return type cannot represent the converted integer value */
cout<< “atoi( “21474836491” )=” << atol( “21474836491” ) << endl ;

Output in Code::Blocks,

atoi( “23.45” )= 23
atol( “2580String” )= 2580
atoll( “23.9e5” )= 23 (number after ‘.’ is neglected)
atol( “F3F33” )= 0
typeid( atoi( “33” ) ).name( )= i
typeid( atol( “3489” ) ).name( )= l
typeid( atoll( “023” ) ).name( )= x

atoi( “21474836491” )= 11

For the last code the ‘int’ type can represent integer only up to ‭21474836487 but the converted value is 21474836491,so int type cannot represent it.Here what happen is only the first 32 bits of 21474836491 is taken.If you look at the bits of 21474836491 which is ‘‭0101 00000000000000000000000000001011‬’,the left most four bits is drop and the remaining bits which is same as ‘1011’ is taken.This gives 11 when converting to integer using the base-2 numeral method.Hence the output is 11.





A more exhaustive code example using atoi , atol and atoll function is shown below.

Link C++ cmath nan: myths and conception

Code example

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std ;

char* func( )
{
return “2300”;
}

int main( )
{
string st=”900″;

const char* c[]=”45.67 “;

//cout<< atol( st ) ;/// error~!
cout<< atoll( c ) << endl ; //work fine

/*Using ‘func()’ */
cout<< atol( func() ) << endl ;

cout<< “asin( atoi(\”23.47\”) )=” << asin( atoi(“23.47”) ) << endl
<< “tgamma( atoll( \”90\” ) )=” << tgamma( atoll( “90” ) ) << endl ;

cin.get( );
return 0 ;
}

Output

45
2300
asin( atoi(“23.47”) )=nan
tgamma( atoll( “90” ) )=1.6508e+136

The ‘asin’ is found in <cmath> and it compute the arc sine of the parameter.

Link : C++ cmath asin , asinf , asinl

The ‘tgamma’ function is also found in <cmath> , and it compute the gamma function of the parameter.

Link : C++ cmath tgamma function


*Side Note

Except for the behavior on error,atoi , atol and atoll is similar to

(int)strtol(nptr, (char **)NULL , 10)

strtol(nptr, (char **)NULL , 10)

strtoll(nptr, (char **)NULL , 10)

 


Related links

->C programming atoi, atol,atoll stdlib.h