Strtol() function in C

Introduction:

The strtol function in the C programming language turns a string into a long integer.

The strtol function skips every character that is white space at the beginning of the string, converts the characters after that as numbers, and then terminates when it reaches the first character that isn't a number.

Syntax

The C language's syntax for the strtol function is as follows:

Arguments or Parameters used for this function:

nptr

A pointer to a string that will be transformed into a long integer.

endptr

The strtol function uses it to specify where the conversion terminated. If endptr is not a null pointer, the strtol function will change endptr such that it now points to the first character that was not transformed.

base

It is the base number, which is being transformed. The base serves as the number's radix, which must fall between 2 and 36. Unless the converted number begins with O (for Octal), Ox (for Hex), or OX (for Hex), the number is believed to be decimal if the base is zero.

Returns

The long integer representation of a string is what the strtol function returns. The strtol function skips every white space character at the beginning of the string, converts the characters after that as numbers, and then terminates when it reaches the first character that isn't a number.

The errno variable will be set to ERANGE if the strtol function attempts to convert a value that is either too large or too little. The function will return LONG_MAX if the value is too large to convert. The function will return a value of LONG_MIN if the input number is too small to convert.

Mandatory Header

The strtol function's necessary header in the C language is:

Refers To

The strtol function is available in the following versions of the C programming language:

Example of strtol

Let's have a look at an example of how the strtol function might be used in a C program:

Output:

This program's output after compilation and execution is:

123 decimal
19e hexadecimal





Latest Courses