Pointer to the null-terminated string to be converted to a long integer.
2nd Parameter Type : char **
A pointer to a pointer to a null-terminated string. Commonly used in functions like `strtol` and `strtok_r` to store the address of the first invalid character or to maintain state across function calls.
2nd Parameter
Pointer to a `char *` that will store the address of the first invalid character after the number in `nptr`. Can be `NULL` if this information is not needed.
3rd Parameter Type : int
Integer type (typically 4 bytes, -2,147,483,648 to 2,147,483,647)
3rd Parameter
The base of the number in `nptr` (e.g., 10 for decimal, 16 for hexadecimal). If 0, the base is automatically detected from the string prefix (`0x` for hex, `0` for octal, or decimal otherwise).
Read more about parameters of strtol in parameters section
The strtolfunction in C language or (String to Long Integer) as the name states, converts the initial portion of the string to a long integer.
The strtol function processes a string, allowing you to specify the number base (e.g., binary, octal, decimal, hexadecimal or any other integer base between 2 and 36 inclusive) and providing detailed error handling through a pointer to the first invalid character. It's output is numerical value of a long int type in case the string passed represents number. The strtol function operates in a manner very similar to that of other functions in it's family, such as strtoul, strtof, strtod, and strtold. Each of these functions follows a consistent pattern: they parse a string, handle optional signs, process valid characters based on the specified format (e.g., digits, decimal points, or exponents), and stop at the first invalid character, returning the converted value up to that point. That way of action makes them conceptually different from another group of functions converting string to number like atoi, atof, and atol that do not handle errors and do not accept a base parameter for number conversion.
To Summarize the Workflow:
1.
Takes a string (like "123abc"), a pointer to store the end of the parsed number, and a base (e.g., 10 for decimal, 16 for hexadecimal).
2.
Scans the string, ignoring leading whitespace (spaces, tabs, newlines).
3.
Checks for an optional sign (+ or -) at the beginning of the string and interprets the number as positive or negative accordingly.
4.
Processes the digits in the string based on the specified base:
5.
- For bases 2 to 36, digits 0-9 and letters A-Z (case-insensitive) are valid.
6.
- If the base is 0, auto-detects the base from the string prefix:
7.
- "0x" or "0X" → Base 16 (hexadecimal).
8.
- "0" → Base 8 (octal).
9.
- No prefix → Base 10 (decimal).
10.
Stops processing at the first invalid character for the given base and stores its location in the provided pointer.
11.
Returns the converted long integer value of the processed digits (e.g., 123).
12.
If the string doesn’t start with a valid number (after optional whitespace and sign), it returns 0.
13.
If the converted value is outside the range of a long int, it sets errno to ERANGE and returns LONG_MAX or LONG_MIN.
Read more about return type and value ofstrtol function in return section.
The strtolfunction takes 3
parameters:
•
const char * `nptr`: Pointer to the null-terminated string to be converted to a long integer. The string can contain: Optional leading whitespace (spaces, tabs, newlines). An optional sign (+ or -). A sequence of digits (and letters for bases > 10).
•
char ** `endptr`: Pointer to a `char *` that will store the address of the first invalid character after the number in `nptr`. Can be `NULL` if this information is not needed. If endptr is not NULL, the function sets it to point to the character where parsing stopped. This is useful for: Detecting where parsing ended. Handling invalid input or trailing characters. If the entire string is valid, endptr will point to the null terminator ( ). Special Cases: If the string is empty or contains no valid digits, endptr is set to the start of the string. If endptr is passed as NULL, the function ignores this parameter.
•
int `base`: The base of the number in `nptr` (e.g., 10 for decimal, 16 for hexadecimal). If 0, the base is automatically detected from the string prefix (`0x` for hex, `0` for octal, or decimal otherwise). The base can be: 0: Auto-detects the base from the string's prefix: 0x or 0X → Base 16 (hexadecimal). 0 → Base 8 (octal). No prefix → Base 10 (decimal). 2 to 36: Explicitly specifies the base. For bases > 10, letters A-Z (case-insensitive) represent values 10–35. If the base is invalid (e.g., 1, 37), the behavior is undefined.
Converts the initial part of the string pointed to by `nptr` to a `long` integer according to the specified `base`. The conversion stops at the first invalid character. If `endptr` is not NULL, it points to the first character after the valid number. Returns the converted value or `LONG_MIN`/`LONG_MAX` on underflow/overflow.
The strtol function return value :
If the string is valid, the function returns the converted number
If the string is invalid (no valid digits), the function returns 0
If the converted value is outside the range of a long int, the function sets errno to ERANGE and returns LONG_MAX or LONG_MIN (depending on the sign of the value)
Output
This example demonstrates basic usage of strtol to convert a string to a long integer and identify the remainder of the string.