Atof (converts a string to a float number)
RelatedFunction
Atoi, atol, strtodd, strtol, strtoul
Header file
# Include
Define functions
Double atof (const char * nptr );
Function Description
Atof () will scan the nptr parameter string, skip the leading space character, and start conversion only when a number or positive or negative sign is encountered, the conversion ends only when a non-number or string ends ('\ 0') and the result is returned. The nptr string can contain positive and negative numbers, decimal points, or E (e) to represent the exponential part, such as 123.456 or 123e-2.
Return Value
Returns the number of floating point types after conversion.
Additional instructions
The atof () is the same as the use of strtodd (nptr, (char **) null.
Example
/* Convert string a and string B into numbers and add them together */
# Include
Main ()
{
Char * A = "-100.23 ";
Char * B = "200e-2 ";
Float C;
C = atof (A) + atof (B );
Printf ("C = %. 2f \ n", c );
}
Run
C =-98.23
============================================
Atoi (convert a string to an integer)
Related functions
Atof, atol, atrtodd, strtol, strtoul
Header file
# Include
Define functions
Int atoi (const char * nptr );
Function Description
Atoi () will scan the nptr parameter string, skip the leading space character, and start conversion only when a number or positive or negative sign is encountered, the conversion ends only when a non-number or string ends ('\ 0') and the result is returned.
Return Value
Returns the number of converted integers.
Additional instructions
Atoi () is the same as using strtol (nptr, (char **) null, 10.
Example
/* Convert string a and string B into numbers and add them together */
# Include
Mian ()
{
Char A [] = "-100 ";
Char B [] = "456 ";
Int C;
C = atoi (A) + atoi (B );
Printf (C = % d \ n ", c );
}
Run
C = 356.
========================================================== ===
Atol (convert string to integer)
Related functions
Atof, atoi, strtodd, strtol, strtoul
Header file
# Include
Define functions
Long atol (const char * nptr );
Function Description
Atol () scans the nptr parameter string and skips the leading space character until conversion is started in case of a number or positive or negative sign, the conversion ends only when a non-number or string ends ('\ 0') and the result is returned.
Return Value
Returns the number of converted long integers.
Additional instructions
Atol () and use strtol (nptr, (char **) null, 10); the results are the same.
Example
/* Convert string a and string B into numbers and add them together */
# Include
Main ()
{
Char A [] = "1000000000 ";
Char B [] = "234567890 ";
Long C;
C = atol (A) + atol (B );
Printf ("C = % d \ n", c );
}
Run
C = 1234567890.
========================================================== =
Gcvt (convert the float number to a string and rounding it out)
Related functions
ECVT, fcvt, sprintf
Header file
# Include
Define functions
Char * gcvt (double number, size_t ndigits, char * BUF );
Function Description
Gcvt () is used to convert the parameter number into an ASCII string. The parameter ndigits indicates the number of digits displayed. Gcvt () differs from ECVT () and fcvt () in that the string converted by gcvt () contains the decimal point or positive or negative sign. If the conversion is successful, the converted string will be placed in the space specified by the Buf pointer.
Return Value
Returns a string pointer, which is the Buf pointer.
Additional instructions
Example
# Include
Main ()
{
Double A = 123.45;
Double B =-1234.56;
Char * PTR;
Int decpt, sign;
Gcvt (A, 5, PTR );
Printf ("A value = % s \ n", PTR );
PTR = gcvt (B, 6, PTR );
Printf ("B value = % s \ n", PTR );
}
Run
A value = 123.45
B value =-1234.56
========================================================== ========
Strtodd (convert a string to a floating point number)
Related functions
Atoi, atol, strtodd, strtol, strtoul
Header file
# Include
Define functions
Double strtodd (const char * nptr, char ** endptr );
Function Description
Strtodd () scans the nptr parameter string and skips the leading space character. Conversion starts only when a number or positive or negative sign is encountered, and ends when a non-number or string ends ('\ 0 ') and return the result. If the endptr value is not null, the character pointer in the nptr that is terminated when an exception occurs is returned by the endptr. The nptr string can contain positive and negative numbers, decimal points, or E (e) to represent the exponent part. Such as 123.456 or 123e-2.
Return Value
Returns the number of floating point types after conversion.
Additional instructions
Refer to atof ().
Example
/* Convert the string A, B, and C into numbers in hexadecimal notation of 10, 2, and 16 */
# Include
Mian ()
{
Char A [] = "1000000000 ";
Char B [] = "1000000000 ";
Char C [] = "FFFF ";
Printf ("A = % d \ n", strtodd (A, null, 10 ));
Printf ("B = % d \ n", strtodd (B, null, 2 ));
Printf ("C = % d \ n", strtodd (C, null, 16 ));
}
Run
A = 1000000000
B = 512
C = 65535.
========================================================== ========
Strtol (converts a string to a growth integer)
Related functions
Atof, atoi, atol, strtodd, strtoul
Header file
# Include
Define functions
Long int strtol (const char * nptr, char ** endptr, int base );
Function Description
Strtol () converts the nptr parameter string to the Growth integer number based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base mode. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal. When the base value is 0, the conversion is made using 10. However, if the '0x 'prefix character is encountered, the hexadecimal conversion is made using 16. At the beginning, strtol () scans the nptr parameter string and skips the leading space character until it starts to convert in case of a number or positive or negative sign, when a non-number or string ends ('\ 0'), the conversion ends and the result is returned. If the endptr parameter is not null, the character pointer in the nptr that is terminated due to an exception is returned by the endptr.
Return Value
Returns the number of converted long integers. Otherwise, an error is returned.CodeStored in errno.
Additional instructions
The conversion string specified by erange exceeds the valid range.
Example
/* Convert the string A, B, and C into numbers in hexadecimal notation of 10, 2, and 16 */
# Include
Main ()
{
Char A [] = "1000000000 ";
Char B [] = "1000000000 ";
Char C [] = "FFFF ";
Printf ("A = % d \ n", strtol (A, null, 10 ));
Printf ("B = % d \ n", strtol (B, null, 2 ));
Printf ("C = % d \ n", strtol (C, null, 16 ));
}
Run
A = 1000000000
B = 512
C = 65535.
========================================================== ==========
strtoul (convert string to unsigned long integer number)
related functions
atof, atoi, atol, strtodd, strtol
header file
# include
definition function
unsigned long int strtoul (const char * nptr, char ** endptr, int base );
function description
strtoul () converts the nptr parameter string to an unsigned long integer value based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base mode. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal. When the base value is 0, the conversion is made using 10. However, if the '0x 'prefix character is encountered, the hexadecimal conversion is made using 16. At the beginning, strtoul () will scan the nptr parameter string and skip the leading space string until conversion starts when a number or positive or negative sign is encountered, when a non-number or string ends ('\ 0'), the conversion ends and the result is returned. If the endptr parameter is not null, the character pointer in the nptr that is terminated due to an exception is returned by the endptr.
return value
return the converted long integer. Otherwise, the error code is returned and stored in errno.
additional description
the conversion string specified by erange is out of the valid range.