Keil Logo

strtol

Summary
#include <stdlib.h>

long strtol (
  const char *string,   /* string to convert */
  char **ptr,           /* ptr to subsequent characters */
  int base);            /* number base for conversion */
Description

The strtol function converts string into a signed long value. The input string is a sequence of characters that can be interpreted as an integer number. Whitespace characters at the beginning of string are skipped. An optional sign may precede the number.

The value of *ptr is set to point to the first character in string immediately following the converted part of string. If ptr is NULL, no value is assigned to *ptr. If no conversion is possible, *ptr is set to the value of string and the value 0 is returned by the strtol function.

If the base is zero, the syntax expected is similar to that of integer constants, which is formed by a succession of:

  • An optional prefix indicating octal ("0") or hexadecimal ("0x") base.
  • A sequence of decimal digits (if no base prefix was specified) or either octal or hexadecimal digits if a specific prefix is present.

The radix of the number is deduced from its format.

If the value of base is in the range 2-36, the number in string must consist of a non-zero sequence of letters and digits representing an integer in the specified base. The letters a-z (or A-Z) represent the values 10-36. Only those letters representing values less than the base are permitted. If the base is 16, the number may begin with 0x or 0X, which is ignored.

The strtol function requires string to have the following format:

whitespace〛 〚{+|-}〛 digits

Where

whitespace May be any whitespace characters.
digits May be one or more decimal digits (0-9).
Return Value

The strtol function returns the integer value that is produced by interpreting the characters in string as a number. The value LONG_MIN or LONG_MAX is returned to indicate an overflow or underflow.

See Also

atof, atoi, atol, strtod, strtoul

Example
#include <stdlib.h>
#include <stdio.h> /* for printf */

void tst_strtol (void) {
  long l;
  char s [] = "-123456789 stuff";
  char *p;

  l = strtol (s, &p, 10);
  printf ("strtol(%s) = %ld\n", s, l);

/* p points to " stuff" */
}
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.