数据类型转换函数strtod | atoi | atol
strtod > Convert string to double
atoi > Convert string to int
atol > Convert string to long int
1、strtod
double strtod ( const char * str, char ** endptr );
Convert string to double
Parses the C string str
interpreting its content as a floating point number and returns its value as a double
. If endptr
is not a null pointer, the function also sets the value pointed by 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 resembling that of floating point literals, 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 strtod
is formed by a succession of:
- An optional plus or minus sign
- A sequence of digits, optionally containing a decimal-point character
- An optional exponent part, which itself consists on an 'e' or 'E' character followed by an optional sign and a sequence of digits.
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.
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 double
value.
If no valid conversion could be performed, a zero value (0.0
) is returned.
If the correct value is out of the range of representable values, a positive or negative HUGE_VAL
is returned, and the global variable errno
is set to ERANGE
.
If the correct value would cause underflow, zero is returned and errno
is set to ERANGE
.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Output:
The moon completes 12.37 orbits per Earth year.
2、atoi
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 an int
value.
The function first discards as many whitespace characters 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 numerical 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.
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 no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, INT_MAX
or INT_MIN
is returned.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Output:
3、atol
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 long int
value.
The function first discards as many whitespace characters 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 numerical 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.
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 correct value is out of the range of representable values, LONG_MAX
or LONG_MIN
is returned.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Output:
Enter a number: 567283 The value entered is 567283. The double is 1134566. |