C Function Atol
## C Library Function - atol()
The `atol()` function is a built-in C library function used to convert a string representation of an integer into a `long int` value. The name stands for **"ASCII to long"**.
This function is defined in the `` header file.
---
## Syntax
```c
long int atol(const char *str);
```
### Parameters
* **`str`**: A pointer to a null-terminated string representing an integer value that you want to convert.
### Return Value
* **Success**: Returns the converted `long int` value.
* **Failure**: If no valid conversion can be performed (e.g., the string starts with non-numeric characters), the function returns `0`.
---
## How it Works
The `atol()` function processes the input string as follows:
1. **Leading Whitespace**: It automatically skips any leading whitespace characters (spaces, tabs, newlines).
2. **Optional Sign**: It identifies an optional plus (`+`) or minus (`-`) sign.
3. **Digits**: It reads the subsequent decimal digits and converts them to a `long int`.
4. **Stopping Condition**: It stops reading as soon as it encounters the first character that is not a valid numerical digit (e.g., letters, punctuation, or the null terminator `\0`).
---
## Code Example
The following example demonstrates how to use the `atol()` function to convert valid numeric strings, handle strings with non-numeric characters, and handle invalid inputs.
```c
#include
#include
#include
int main()
{
long val;
char str;
// Case 1: Standard numeric string conversion
strcpy(str, "98993489");
val = atol(str);
printf("String value = \"%s\", Long Int value = %ld\n", str, val);
// Case 2: Invalid numeric string
strcpy(str, "youtip.co");
val = atol(str);
printf("String value = \"%s\", Long Int value = %ld\n", str, val);
// Case 3: String starting with numbers followed by letters
strcpy(str, "123456xyz");
val = atol(str);
printf("String value = \"%s\", Long Int value = %ld\n", str, val);
return 0;
}
```
### Output
```text
String value = "98993489", Long Int value = 98993489
String value = "youtip.co", Long Int value = 0
String value = "123456xyz", Long Int value = 123456
```
---
## Important Considerations & Limitations
While `atol()` is simple and easy to use, it has several limitations that developers should be aware of:
### 1. No Error Detection for Overflow
If the converted value exceeds the range of a `long int` (either too large or too small), the behavior is **undefined**. The function does not set `errno` or provide any indication of overflow.
### 2. Ambiguity on Failure
If `atol()` returns `0`, it is impossible to distinguish between a failed conversion (e.g., `"hello"`) and a successful conversion of the actual string `"0"`.
### 3. Modern Alternative: `strtol()`
For robust production code, it is highly recommended to use **`strtol()`** (string to long) instead of `atol()`.
`strtol()` provides:
* **Error checking**: It sets `errno` to `ERANGE` on overflow.
* **End pointer**: It returns a pointer to the first character after the parsed number, allowing you to detect trailing garbage or verify if the entire string was successfully parsed.
* **Base support**: It allows parsing numbers in octal (base 8), hexadecimal (base 16), or any other base up to 36.
YouTip