atol in c
atol in c programming

If you want to know more on atol in c then read this blog post.

What is atol in c programming?

atol is a function which converts the given string to an long integer.

This function returns integral number as a long int. If conversion is not possible it returns zero.

This function is defined in stdlib.h header file.

atol function skips all whitespace characters at the beginning of the string and converts the character to a long integer.

atol function stops the process when it encounters the character that isn’t a number.

Prototype of atol

#include<stdlib.h>
long int atol(const char* string);

Parameters of atol in c

atol function takes string as an argument.

Return value of atol in c

atol function returns long integer value. If conversion not possible then it returns zero.


Example program for atol in c

Example 1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
 long result;
 char string[40]="123456";
 result=atol(string);
 printf("The result is %ld \n",result);
 return 0;
}
example for atol function in c 1
example for atol function in c

Code explanation

The given input string is converted in to long integer by calling atol function. The result is being printed onto the console.


Example 2

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
 long result;
 char string[40]="scholarsoul";
 result=atol(string);
 printf("The result is %ld \n",result);
 return 0;
}
example for atol function in c 2
example 2 for atol function in c

Code explanation

The given input string is converted in to long integer by calling atol function. The result is being printed onto the console.

atol() Man page


Difference between atol and itoa in c

atol is a function that converts the given string to a long integer. itoa function in c converts an integer to string using a given base.

atol returns integral number as a long int. If the conversion is not possible it returns zero. itoa function returns pointer to the resulting string.

atol takes one argument. itoa function takes three arguments.

atol function stops the process when it encounters the character that isn’t a number. itoa function terminates the process when it encounters a null value.

atol in citoa in c
Converts the given string to an long integer.Converts integer to string using a given base.
long int atol(const char* string);char *itoa(int value, char *buffer,int radix);
Takes one argument.Takes three arguments.
Difference between atol and itoa in c programming

People also ask for

What does atol stands for?

atol stands for ASCII to long integer. It is included in stdlib.h header file.

What is atol in c?

atol is a function that converts a string to a long integer. It is defined in stdlib.h header file.

How does atol works?

atol function takes input as a string and converts it to a long integer. If it encounters a character that is not a number then it stops the process.