Pointer to a null-terminated string representing a long integer value
Read more about parameters of atol in parameters section
The atolfunction in C language converts a string to a long integer.
The atol function converts the initial portion of the string pointed to by str to a long integer representation. It operates similarly to atoi, but returns a long int instead of an int. It skips any whitespace characters at the beginning of the string, then takes an optional plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value. Important: atol function does not recognize decimal point as a decimal point and treats it just as another non-digit character which causes it to break the loop.
To Summarize the Workflow:
1.
Takes a string (like "-123456789") as input.
2.
Scans the string, ignoring leading whitespace.
3.
If the string starts with a + or - sign, it interprets the number as positive or negative accordingly.
4.
In case no sign found-sets result to positive as default
5.
Then processes the character representations of digits (if present).
6.
Stops at the first non-digit character (like a letter, symbol, or decimal point).
7.
Does not recognize decimal points as part of the number.
8.
Returns the long integer value of the processed digits (e.g., -123456789).
9.
If the string doesn’t start with a valid number, it returns 0.
Read more about return type and value ofatol function in return section.
The atolfunction takes 1
parameter:
•
const char * `str`: Pointer to a null-terminated string representing a long integer value
Converts the initial portion of a string to a `long` value, interpreting the content as a decimal integer.
The atol function return value :
Returns the converted long integer value
If no valid conversion could be performed, it returns 0L
Output
This example demonstrates the basic usage of the `atol` function with different input strings, including a positive number, a negative number with leading whitespace, a very large number, and an invalid input.