C语言字符转数字函数

文章目录

atof

c 复制代码
double atof (const char* str);

Convert string to double

Parses the C string str, interpreting its content as a floating point number and returns its value as a double.

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals (see below), and interprets them as a numerical value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this function.
C90 (C++98)

A valid floating point number for atof using the "C" locale is formed by an optional sign character (+ or -), followed by a sequence of digits, optionally containing a decimal-point character (.), optionally followed by an exponent part (an e or E character followed by an optional sign and a sequence of digits).
C99/C11 (C++11)

A valid floating point number for atof using the "C" locale is formed by an optional sign character (+ or -), followed by one of:

  • A sequence of digits, optionally containing a decimal-point character (.), optionally followed by an exponent part (an e or E character followed by an optional sign and a sequence of digits).
  • A 0x or 0X prefix, then a sequence of hexadecimal digits (as in isxdigit) optionally containing a period which separates the whole and fractional number parts. Optionally followed by a power of 2 exponent (a p or P character followed by an optional sign and a sequence of hexadecimal digits).
  • INF or INFINITY (ignoring case).
  • NAN or NANsequence (ignoring case), where sequence is a sequence of characters, where each character is either an alphanumeric character (as in isalnum) or the underscore character (_).

If the first sequence of non-whitespace characters in str does not form a valid floating-point number as just defined, or if no such sequence exists because either str is empty or contains only whitespace characters, no conversion is performed and the function returns 0.0.

Parameters
str

C-string beginning with the representation of a floating-point number.
Return Value

On success, the function returns the converted floating point number as a double value.

If no valid conversion could be performed, the function returns zero (0.0).

If the converted value would be out of the range of representable values by a double, it causes undefined behavior. See strtod for a more robust cross-platform alternative when this is a possibility.

Data races

The array pointed by str is accessed.
Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if the converted value would be out of the range of values representable by a double, it causes undefined behavior.

atoi

c 复制代码
int atoi (const char * str);

Convert string to integer

Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int.

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.

Parameters
str

C-string beginning with the representation of an integral number.

Return Value

On success, the function returns the converted integral number as an int value.

If the converted value would be out of the range of representable values by an int, it causes undefined behavior. See strtol for a more robust cross-platform alternative when this is a possibility.

Data races

The array pointed by str is accessed.

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if the converted value would be out of the range of values representable by an int, it causes undefined behavior.

atol

c 复制代码
long int atol ( const char * str );

Convert string to long integer

Parses the C-string str interpreting its content as an integral number, which is returned as a value of type long int.

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.

Parameters
str

C-string containing the representation of an integral number.
Return Value

On success, the function returns the converted integral number as a long int value.

If no valid conversion could be performed, a zero value is returned.

If the converted value would be out of the range of representable values by a long int, it causes undefined behavior. See strtol for a more robust cross-platform alternative when this is a possibility.

Data races

The array pointed by str is accessed.

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if the converted value would be out of the range of values representable by an long int, it causes undefined behavior.

atoll

c 复制代码
long long int atoll ( const char * str );

Convert string to long long integer

Parses the C-string str interpreting its content as an integral number, which is returned as a value of type long long int.

This function operates like atol to interpret the string, but produces numbers of type long long int (see atol for details on the interpretation process).
Parameters
str

C-string containing the representation of an integral number.
Return Value

On success, the function returns the converted integral number as a long long int value.

If no valid conversion could be performed, a zero value is returned.

If the converted value would be out of the range of representable values by a long long int, it causes undefined behavior. See strtoll for a more robust cross-platform alternative when this is a possibility.

Data races

The array pointed by str is accessed.

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if the converted value would be out of the range of values representable by a long long int, it causes undefined behavior.

strtod

c 复制代码
double strtod (const char* str, char** endptr);

Convert string to double

Parses the C-string str interpreting its content as a floating point number (according to the current locale) and returns its value as a double. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals (see below), and interprets them as a numerical value. A pointer to the rest of the string after the last valid character is stored in the object pointed by endptr.
C90 (C++98)

A valid floating point number for strtod using the "C" locale is formed by an optional sign character (+ or -), followed by a sequence of digits, optionally containing a decimal-point character (.), optionally followed by an exponent part (an e or E character followed by an optional sign and a sequence of digits).
C99/C11 (C++11)

A valid floating point number for strtod using the "C" locale is formed by an optional sign character (+ or -), followed by one of:

  • A sequence of digits, optionally containing a decimal-point character (.), optionally followed by an exponent part (an e or E character followed by an optional sign and a sequence of digits).
  • A 0x or 0X prefix, then a sequence of hexadecimal digits (as in isxdigit) optionally containing a period which separates the whole and fractional number parts. Optionally followed by a power of 2 exponent (a p or P character followed by an optional sign and a sequence of hexadecimal digits).
  • INF or INFINITY (ignoring case).
  • NAN or NANsequence (ignoring case), where sequence is a sequence of characters, where each character is either an alphanumeric character (as in isalnum) or the underscore character (_).

If the first sequence of non-whitespace characters in str does not form a valid floating-point number as just described, or if no such sequence exists because either str is empty or contains only whitespace characters, no conversion is performed and the function returns a zero value.

Parameters
str

C-string beginning with the representation of a floating-point number.
endptr

Reference to an already allocated object of type char*, whose value is set by the function to the next character in str after the numerical value.

This parameter can also be a null pointer, in which case it is not used.

Return Value

On success, the function returns the converted floating point number as a value of type double.

If no valid conversion could be performed, the function returns zero (0.0).

If the correct value is out of the range of representable values for the type, a positive or negative HUGE_VAL is returned, and errno is set to ERANGE.

C90 (C++98)

If the correct value would cause underflow, the function returns a value whose magnitude is no greater than the smallest normalized positive number and sets errno to ERANGE.
C99/C11 (C++11)

If the correct value would cause underflow, the function returns a value whose magnitude is no greater than the smallest normalized positive number (some library implementations may also set errno to ERANGE in this case).

Data races

The array pointed by str is accessed, and the pointer pointed by endptr is modified (if not null).

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.

strtof

c 复制代码
float strtof (const char* str, char** endptr);

Convert string to float

Parses the C-string str interpreting its content as a floating point number (according to the current locale) and returns its value as a float. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals (see below), and interprets them as a numerical value. A pointer to the rest of the string after the last valid character is stored in the object pointed by endptr.

A valid floating point number for strtof using the "C" locale is formed by an optional sign character (+ or -), followed by one of:

A sequence of digits, optionally containing a decimal-point character (.), optionally followed by an exponent part (an e or E character followed by an optional sign and a sequence of digits).

A 0x or 0X prefix, then a sequence of hexadecimal digits (as in isxdigit) optionally containing a period which separates the whole and fractional number parts. Optionally followed by a power of 2 exponent (a p or P character followed by an optional sign and a sequence of hexadecimal digits).

INF or INFINITY (ignoring case).

NAN or NANsequence (ignoring case), where sequence is a sequence of characters, where each character is either an alphanumeric character (as in isalnum) or the underscore character (_).

If the first sequence of non-whitespace characters in str does not form a valid floating-point number as just described, or if no such sequence exists because either str is empty or contains only whitespace characters, no conversion is performed and the function returns 0.0F.

Parameters
str

C-string beginning with the representation of a floating-point number.
endptr

Reference to an already allocated object of type char*, whose value is set by the function to the next character in str after the numerical value.

This parameter can also be a null pointer, in which case it is not used.

Return Value

On success, the function returns the converted floating point number as a value of type float.

If no valid conversion could be performed, the function returns zero (0.0F).

If the correct value is out of the range of representable values for the type, a positive or negative HUGE_VALF is returned, and errno is set to ERANGE.

If the correct value would cause underflow, the function returns a value whose magnitude is no greater than the smallest normalized positive number (some library implementations may also set errno to ERANGE in this case).

Data races

The array pointed by str is accessed, and the pointer pointed by endptr is modified (if not null).

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.

strtol

c 复制代码
long int strtol (const char* str, char** endptr, int base);

Convert string to long integer

Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a long int value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax that depends on the base parameter, and interprets them as a numerical value. Finally, a pointer to the first character following the integer representation in str is stored in the object pointed by endptr.

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

An optional sign character (+ or -)

An optional prefix indicating octal or hexadecimal base ("0" or "0x"/"0X" respectively)

A sequence of decimal digits (if no base prefix was specified) or either octal or hexadecimal digits if a specific prefix is present

If the base value is between 2 and 36, the format expected for the integral number is a succession of any of the valid digits and/or letters needed to represent integers of the specified radix (starting from '0' and up to 'z'/'Z' for radix 36). The sequence may optionally be preceded by a sign (either + or -) and, if base is 16, an optional "0x" or "0X" prefix.

If the first sequence of non-whitespace characters in str is not a valid integral number as defined above, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

For locales other than the "C" locale, additional subject sequence forms may be accepted.

Parameters
str

C-string beginning with the representation of an integral number.
endptr

Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.

This parameter can also be a null pointer, in which case it is not used.
base

Numerical base (radix) that determines the valid characters and their interpretation.

If this is 0, the base used is determined by the format in the sequence (see above).

Return Value

On success, the function returns the converted integral number as a long int value.

If no valid conversion could be performed, a zero value is returned (0L).

If the value read is out of the range of representable values by a long int, the function returns LONG_MAX or LONG_MIN (defined in ), and errno is set to ERANGE.

Data races

The array pointed by str is accessed, and the pointer pointed by endptr is modified (if not null).

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.

strtold

c 复制代码
long double strtold (const char* str, char** endptr);

Convert string to long double

Parses the C string str interpreting its content as a floating point number (according to the current locale) and returns its value as a long double. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

This function operates like strtod to interpret the string, but produces numbers of type long double (see strtod for details on the interpretation process).
Parameters
str

C string beginning with the representation of a floating-point number.
endptr

Reference to an already allocated object of type char*, whose value is set by the function to the next character in str after the numerical value.

This parameter can also be a null pointer, in which case it is not used.
Return Value

On success, the function returns the converted floating point number as a value of type long double.

If no valid conversion could be performed, the function returns zero (0.0L).

If the correct value is out of the range of representable values for the type, a positive or negative HUGE_VALL is returned, and errno is set to ERANGE.

If the correct value would cause underflow, the function returns a value whose magnitude is no greater than the smallest normalized positive number (some library implementations may also set errno to ERANGE in this case).
Data races

The array pointed by str is accessed, and the pointer pointed by endptr is modified (if not null).

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.

strtoll

c 复制代码
long long int strtoll (const char* str, char** endptr, int base);

Convert string to long long integer

Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a value of type long long int. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

This function operates like strtol to interpret the string, but produces numbers of type long long int (see strtol for details on the interpretation process).
Parameters
str

C-string beginning with the representation of an integral number.
endptr

Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.

This parameter can also be a null pointer, in which case it is not used.
base

Numerical base (radix) that determines the valid characters and their interpretation.

If this is 0, the base used is determined by the format in the sequence (see strtol for details).

Return Value

On success, the function returns the converted integral number as a long long int value.

If no valid conversion could be performed, a zero value is returned (0LL).

If the value read is out of the range of representable values by a long long int, the function returns LLONG_MAX or LLONG_MIN (defined in ), and errno is set to ERANGE.

Data races

The array pointed by str is accessed, and the pointer pointed by endptr is modified (if not null).

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.

strtoul

c 复制代码
unsigned long int strtoul (const char* str, char** endptr, int base);

Convert string to unsigned long integer

Parses the C-string str, interpreting its content as an integral number of the specified base, which is returned as an value of type unsigned long int.

This function operates like strtol to interpret the string, but produces numbers of type unsigned long int (see strtol for details on the interpretation process).
Parameters
str

C-string containing the representation of an integral number.
endptr

Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.

This parameter can also be a null pointer, in which case it is not used.
base

Numerical base (radix) that determines the valid characters and their interpretation.

If this is 0, the base used is determined by the format in the sequence (see strtol for details).

Return Value

On success, the function returns the converted integral number as an unsigned long int value.

If no valid conversion could be performed, a zero value is returned.

If the value read is out of the range of representable values by an unsigned long int, the function returns ULONG_MAX (defined in ), and errno is set to ERANGE.

Data races

The array pointed by str is accessed, and the pointer pointed by endptr is modified (if not null).

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.

strtoull

c 复制代码
unsigned long long int strtoull (const char* str, char** endptr, int base);

Convert string to unsigned long long integer

Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a value of type unsigned long long int. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

This function operates like strtol to interpret the string, but produces numbers of type unsigned long long int (see strtol for details on the interpretation process).
Parameters
str

C-string beginning with the representation of an integral number.
endptr

Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.

This parameter can also be a null pointer, in which case it is not used.
base

Numerical base (radix) that determines the valid characters and their interpretation.

If this is 0, the base used is determined by the format in the sequence (see strtol for details).

Return Value

On success, the function returns the converted integral number as an unsigned long long int value.

If no valid conversion could be performed, a zero value is returned (0ULL).

If the value read is out of the range of representable values by an unsigned long long int, the function returns ULLONG_MAX (defined in ), and errno is set to ERANGE.

Data races

The array pointed by str is accessed, and the pointer pointed by endptr is modified (if not null).

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

If str does not point to a valid C-string, or if endptr does not point to a valid pointer object, it causes undefined behavior.

相关推荐
汤姆和杰瑞在瑞士吃糯米粑粑8 分钟前
【C++学习篇】AVL树
开发语言·c++·学习
Biomamba生信基地15 分钟前
R语言基础| 功效分析
开发语言·python·r语言·医药
手可摘星河17 分钟前
php中 cli和cgi的区别
开发语言·php
CT随41 分钟前
Redis内存碎片详解
java·开发语言
anlog1 小时前
C#在自定义事件里传递数据
开发语言·c#·自定义事件
奶香臭豆腐1 小时前
C++ —— 模板类具体化
开发语言·c++·学习
晚夜微雨问海棠呀1 小时前
长沙景区数据分析项目实现
开发语言·python·信息可视化
graceyun1 小时前
C语言初阶习题【9】数9的个数
c语言·开发语言
波音彬要多做2 小时前
41 stack类与queue类
开发语言·数据结构·c++·学习·算法
Swift社区2 小时前
Excel 列名称转换问题 Swift 解答
开发语言·excel·swift