Use C atol function to convert string to long type

Syntax

C atol function has the following syntax.

long int atol(const char *str);

C atol function is from header file stdlib.h.

Description

C atol function converts the string into a long int value.

Example

Use C atol function to convert string to long


#include <stdlib.h>
#include <stdio.h>
/*from www.ja v  a 2 s  .c  o m*/
int main(void)
{
  char num1[80], num2[80];

  printf("Enter first: ");
  gets(num1);
  printf("Enter second: ");
  gets(num2);
  printf("The sum is: %ld.", atol(num1)+atol(num2));

  return 0;
}

The code above generates the following result.





















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h