Node:Parsing of Integers, Next:Parsing of Floats, Up:Parsing of Numbers
The str
functions are declared in stdlib.h
and those
beginning with wcs
are declared in wchar.h
. One might
wonder about the use of restrict
in the prototypes of the
functions in this section. It is seemingly useless but the ISO C
standard uses it (for the functions defined there) so we have to do it
as well.
long int strtol (const char *restrict string, char **restrict tailptr, int base) | Function |
The strtol ("string-to-long") function converts the initial
part of string to a signed integer, which is returned as a value
of type long int .
This function attempts to decompose string as follows:
If the string is empty, contains only whitespace, or does not contain an
initial substring that has the expected syntax for an integer in the
specified base, no conversion is performed. In this case,
In a locale other than the standard If the string has valid syntax for an integer but the value is not
representable because of overflow, You should not check for errors by examining the return value of
There is an example at the end of this section. |
long int wcstol (const wchar_t *restrict string, wchar_t **restrict tailptr, int base) | Function |
The wcstol function is equivalent to the strtol function
in nearly all aspects but handles wide character strings.
The |
unsigned long int strtoul (const char *retrict string, char **restrict tailptr, int base) | Function |
The strtoul ("string-to-unsigned-long") function is like
strtol except it converts to an unsigned long int value.
The syntax is the same as described above for strtol . The value
returned on overflow is ULONG_MAX (see Range of Type).
If string depicts a negative number,
|
unsigned long int wcstoul (const wchar_t *restrict string, wchar_t **restrict tailptr, int base) | Function |
The wcstoul function is equivalent to the strtoul function
in nearly all aspects but handles wide character strings.
The |
long long int strtoll (const char *restrict string, char **restrict tailptr, int base) | Function |
The strtoll function is like strtol except that it returns
a long long int value, and accepts numbers with a correspondingly
larger range.
If the string has valid syntax for an integer but the value is not
representable because of overflow, The |
long long int wcstoll (const wchar_t *restrict string, wchar_t **restrict tailptr, int base) | Function |
The wcstoll function is equivalent to the strtoll function
in nearly all aspects but handles wide character strings.
The |
long long int strtoq (const char *restrict string, char **restrict tailptr, int base) | Function |
strtoq ("string-to-quad-word") is the BSD name for strtoll .
|
long long int wcstoq (const wchar_t *restrict string, wchar_t **restrict tailptr, int base) | Function |
The wcstoq function is equivalent to the strtoq function
in nearly all aspects but handles wide character strings.
The |
unsigned long long int strtoull (const char *restrict string, char **restrict tailptr, int base) | Function |
The strtoull function is related to strtoll the same way
strtoul is related to strtol .
The |
unsigned long long int wcstoull (const wchar_t *restrict string, wchar_t **restrict tailptr, int base) | Function |
The wcstoull function is equivalent to the strtoull function
in nearly all aspects but handles wide character strings.
The |
unsigned long long int strtouq (const char *restrict string, char **restrict tailptr, int base) | Function |
strtouq is the BSD name for strtoull .
|
unsigned long long int wcstouq (const wchar_t *restrict string, wchar_t **restrict tailptr, int base) | Function |
The wcstouq function is equivalent to the strtouq function
in nearly all aspects but handles wide character strings.
The |
intmax_t strtoimax (const char *restrict string, char **restrict tailptr, int base) | Function |
The strtoimax function is like strtol except that it returns
a intmax_t value, and accepts numbers of a corresponding range.
If the string has valid syntax for an integer but the value is not
representable because of overflow, See Integers for a description of the |
intmax_t wcstoimax (const wchar_t *restrict string, wchar_t **restrict tailptr, int base) | Function |
The wcstoimax function is equivalent to the strtoimax function
in nearly all aspects but handles wide character strings.
The |
uintmax_t strtoumax (const char *restrict string, char **restrict tailptr, int base) | Function |
The strtoumax function is related to strtoimax
the same way that strtoul is related to strtol .
See Integers for a description of the |
uintmax_t wcstoumax (const wchar_t *restrict string, wchar_t **restrict tailptr, int base) | Function |
The wcstoumax function is equivalent to the strtoumax function
in nearly all aspects but handles wide character strings.
The |
long int atol (const char *string) | Function |
This function is similar to the strtol function with a base
argument of 10 , except that it need not detect overflow errors.
The atol function is provided mostly for compatibility with
existing code; using strtol is more robust.
|
int atoi (const char *string) | Function |
This function is like atol , except that it returns an int .
The atoi function is also considered obsolete; use strtol
instead.
|
long long int atoll (const char *string) | Function |
This function is similar to atol , except it returns a long
long int .
The |
All the functions mentioned in this section so far do not handle
alternative representations of characters as described in the locale
data. Some locales specify thousands separator and the way they have to
be used which can help to make large numbers more readable. To read
such numbers one has to use the scanf
functions with the '
flag.
Here is a function which parses a string as a sequence of integers and
returns the sum of them:
int sum_ints_from_string (char *string) { int sum = 0; while (1) { char *tail; int next; /* Skip whitespace by hand, to detect the end. */ while (isspace (*string)) string++; if (*string == 0) break; /* There is more nonwhitespace, */ /* so it ought to be another number. */ errno = 0; /* Parse it. */ next = strtol (string, &tail, 0); /* Add it in, if not overflow. */ if (errno) printf ("Overflow\n"); else sum += next; /* Advance past it. */ string = tail; } return sum; }