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.

相关推荐
菜鸡且互啄6942 分钟前
在线教育平台,easyexcel使用案例
java·开发语言
电饭叔2 小时前
《python程序语言设计》2018版第5章第52题利用turtle绘制sin函数
开发语言·python
weixin_452600692 小时前
如何为老化的汽车铅酸电池充电
开发语言·单片机·安全·汽车·电机·电源模块·充电桩
Java资深爱好者3 小时前
如何在std::map中查找元素
开发语言·c++
YCCX_XFF213 小时前
ImportError: DLL load failed while importing _imaging: 操作系统无法运行 %1
开发语言·python
哥廷根数学学派4 小时前
基于Maximin的异常检测方法(MATLAB)
开发语言·人工智能·深度学习·机器学习
杰哥在此5 小时前
Java面试题:讨论持续集成/持续部署的重要性,并描述如何在项目中实施CI/CD流程
java·开发语言·python·面试·编程
Unity打怪升级5 小时前
Laravel: 优雅构建PHP应用的现代框架
开发语言·php·laravel
C.C5 小时前
java IO流(1)
java·开发语言
安步当歌6 小时前
【FFmpeg】av_write_trailer函数
c语言·c++·ffmpeg·视频编解码·video-codec