C++ Standard Library C++ STL Library

C++ <cstdlib> - atoll() Function



The C++ <cstdlib> atoll() function is used to interpret an integer value in a byte string pointed to by str.

The function first discards any whitespace characters until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by numeric digits. The string may contain additional characters after those that form the integral number, which are ignored.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists (because either empty or contains only whitespaces), no conversion is performed and zero is returned.

Syntax

long long int atoll ( const char * str );

Parameters

str Specify pointer to the null-terminated byte string to be interpreted.

Return Value

On success, the function returns the converted integral number as a long long int value. If no conversion can be performed, 0 is returned. If the converted value falls out of the range of long long int type, the return value is undefined.

Example:

The example below shows the usage of <cstdlib> atoll() function.

#include <cstdio>
#include <cstdlib>

int main (){
  char str1[] = "123";
  char str2[] = "10.55";
  char str3[] = "100 some words";
  char str4[] = "some words 555";

  long long int num1 = atoll(str1);
  long long int num2 = atoll(str2);
  long long int num3 = atoll(str3);
  long long int num4 = atoll(str4);

  //displaying the result
  printf("atoll(\"%s\") = %lld\n", str1, num1);
  printf("atoll(\"%s\") = %lld\n", str2, num2);
  printf("atoll(\"%s\") = %lld\n", str3, num3);
  printf("atoll(\"%s\") = %lld\n", str4, num4);

  return 0;
}

The output of the above code will be:

atoll("123") = 123
atoll("10.55") = 10
atoll("100 some words") = 100
atoll("some words 555") = 0

❮ C++ <cstdlib> Library

5