Names specified here
Name Description Notes Source Availability
atof() Convert string to real (·) <stdlib.h> C89 C90 C95 C99 C11
atoi() Convert string to integer (·) <stdlib.h> C89 C90 C95 C99 C11
atol() Convert string to integer (·) <stdlib.h> C89 C90 C95 C99 C11
atoll() Convert string to integer (·) <stdlib.h> C99 C11
nan() Compute NaN from string (·) <math.h> C99 C11
nanf() Compute NaN from string (·) <math.h> C99 C11
nanl() Compute NaN from string (·) <math.h> C99 C11
strtod() Convert string to real (·) <stdlib.h> C89 C90 C95 C99 C11
strtof() Convert string to real (·) <stdlib.h> C99 C11
strtoimax() Convert string to integer (·) <inttypes.h> C99 C11
strtol() Convert string to integer (·) <stdlib.h> C89 C90 C95 C99 C11
strtold() Convert string to real (·) <stdlib.h> C99 C11
strtoll() Convert string to integer (·) <stdlib.h> C99 C11
strtoul() Convert string to integer (·) <stdlib.h> C89 C90 C95 C99 C11
strtoull() Convert string to integer (·) <stdlib.h> C99 C11
strtoumax() Convert string to integer (·) <inttypes.h> C99 C11
wcstod() Convert wide-character string to real (·) <wchar.h> C95 C99 C11
wcstof() Convert wide-character string to real (·) <wchar.h> C99 C11
wcstoimax() Convert wide-character string to integer (·) <inttypes.h> C99 C11
wcstol() Convert wide-character string to integer (·) <wchar.h> C95 C99 C11
wcstold() Convert wide-character string to real (·) <wchar.h> C99 C11
wcstoll() Convert wide-character string to integer (·) <wchar.h> C99 C11
wcstoul() Convert wide-character string to integer (·) <wchar.h> C95 C99 C11
wcstoull() Convert wide-character string to integer (·) <wchar.h> C99 C11
wcstoumax() Convert wide-character string to integer (·) <inttypes.h> C99 C11

The following functions take a null-terminated string of bytes or wide characters starting at s, and scan it to interpret it as a number, whose value is returned as an integer or a real floating-point. All functions start by skipping white space, as determined by isspace or iswspace. Most functions have a parameter ep, and if it is not NULL, *ep is set to point to the next character after the those that are interpreted as a number, allowing the remainder of the string to be used for other purposes.

Conversion from string to integer

#include <stdlib.h>
unsigned long strtoul(const char *s, char **ep, int b);
unsigned long long strtoull(const char *s, char **ep, int b);
long strtol(const char *s, char **ep, int b);
long long strtoll(const char *s, char **ep, int b);
#include <inttypes.h>
uintmax_t strtoumax(const char *s, char **ep, int b);
intmax_t strtoimax(const char *s, char **ep, int b);

Functions with names matching ^str[a-z] might be added to <stdlib.h>.

#include <wchar.h>
unsigned long wcstoul(const wchar_t *s, wchar_t **ep, int b);
unsigned long long wcstoull(const wchar_t *s, wchar_t **ep, int b);
long wcstol(const wchar_t *s, wchar_t **ep, int b);
long long wcstoll(const wchar_t *s, wchar_t **ep, int b);
#include <inttypes.h>
uintmax_t wcstoumax(const wchar_t *s, wchar_t **ep, int b);
intmax_t wcstoimax(const wchar_t *s, wchar_t **ep, int b);

These functions parse an integer in base b. After skipping initial white space, they accept an optional sign + or -, an optional type prefix 0x or 0X if b is 16, and then one or more digits from the first b characters of the following list, or their upper-case equivalents:

0123456789abcdefghijklmnopqrstuvwxyz

b must be in the range 2 to 36, or the special value 0. The special value indicates that the base is to be chosen by the type prefix. If it is 0x or 0X, the base is taken to be 16. If it is 0, the base is taken to be 8. Otherwise, the base is taken to be 10.

The functions return the converted number if it has the expected form. If not, they return zero, and *ep is set to s if ep is not NULL. The *tou* functions convert the result to an unsigned value first. If the result overflows, each function sets errno to ERANGE, and returns the largest value of its return type, LONG_MAX, ULONG_MAX, LLONG_MAX or ULLONG_MAX.

#include <stdlib.h>
int atoi(const char *s);
long atol(const char *s);
long long atoll(const char *s);

atoi(s) is equivalent to (int) strtol(s, NULL, 10).

atol(s) is equivalent to strtol(s, NULL, 10).

atoll(s) is equivalent to strtoll(s, NULL, 10).

Conversion from string to real number

#include <stdlib.h>
float strtof(const char *s, char **ep);
double strtod(const char *s, char **ep);
long double strtold(const char *s, char **ep);

Functions with names matching ^str[a-z] might be added to <stdlib.h>.

#include <wchar.h>
float wcstof(const wchar_t *s, wchar_t **ep);
double wcstod(const wchar_t *s, wchar_t **ep);
long double wcstold(const wchar_t *s, wchar_t **ep);

These function parse a real number in decimal or hexadecimal. After skipping initial white space, they accept an optional sign + or -, an optional type prefix 0x or 0X indicating that the format is hexadecimal, at least one digit of the right base, with at most one decimal point, and an optional exponent. The exponent begins with e or E for decimal, and p or P for hexadecimal. It is followed by an optional sign + or -, and at least one decimal digit, even for hexadecimal numbers.

The functions return the converted number if it has the expected form. If not, they return zero, and *ep is set to s if ep is not NULL. If the result overflows, each function sets errno to ERANGE, and returns one of HUGE_VALF, HUGE_VAL or HUGE_VALL according to its return type. If the result underflows, errno is set to ERANGE, and a very small value is returned, but with the correct sign.

These functions also accept INF or INFINITY as a representation of infinity, and return INFINITY if the type supports it, or behave as if the value overflows. They also accept NAN and NAN(n-char-sequenceopt) as a quiet NaN, if supported. The enclosed character sequence must consist only of the following characters:

0 1 2 3 4 5 6 7 8 9 _
a b c d e f g h i j k l m
n o p w r s t u v w x y z
A B C D E F G H I J K L M
N O P W R S T U V W X Y Z
n-char-sequence
n-char
n-char-sequence n-char
n-char
any of the characters 09
any of the characters AZ
any of the characters az
_
underscore
#include <stdlib.h>
double atof(const char *s, char **ep);

atof(s) is equivalent to strtod(s, NULL, 10).

#include <math.h>
float nanf(const char *str);
double nan(const char *str);
long double nanl(const char *str);

These functions take a string and treat it as a parameter for generating a quiet NaN, if supported. A valid string consists only of characters from n-char-sequence.

Passing an empty string "" indicates that the parameter is empty. nanf("") is equivalent to strtof("NAN()", NULL). nan("") is equivalent to strtod("NAN()", NULL). nanl("") is equivalent to strtold("NAN()", NULL). Passing an invalid string indicates that no parameter shall be used to generate the NaN.


CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000