Atof (converts a string to a float number)
Related functions
Atoi, atol, strtodd, strtol, strtoul
Header file
# Include <stdlib. h>
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, and then when a non-number or string ends ('') and return the result. 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 <stdlib. h>
Main ()
{
Char * A = "-100.23 ";
Char * B = "200e-2 ";
Float C;
C = atof (A) + atof (B );
Printf ("C = %. 2f", c );
}
Run
C =-98.23
Atoi (convert a string to an integer)
Related functions
Atof, atol, atrtodd, strtol, strtoul
Header file
# Include <stdlib. h>
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, and then when a non-number or string ends ('') and return the result.
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 <stdlib. h>
Mian ()
{
Char A [] = "-100 ";
Char B [] = "456 ";
Int C;
C = atoi (A) + atoi (B );
Printf (C = % d ", c );
}
Run
C = 356.
Atol (convert string to integer)
Related functions
Atof, atoi, strtodd, strtol, strtoul
Header file
# Include <stdlib. h>
Define functions
Long atol (const char * nptr );
Function Description
Atol () scans the nptr parameter string and skips the leading space character until it starts to convert into a number or a positive or negative sign, and then ends with a non-number or string ('') and return the result.
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 <stdlib. h>
Main ()
{
Char A [] = "1000000000 ";
Char B [] = "234567890 ";
Long C;
C = atol (A) + atol (B );
Printf ("C = % d", c );
}
Run
C = 1234567890.
Gcvt (convert the float number to a string and rounding it out)
Related functions
ECVT, fcvt, sprintf
Header file
# Include <stdlib. h>
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 <stdlib. h>
Main ()
{
Double A = 123.45;
Double B =-1234.56;
Char * PTR;
Int decpt, sign;
Gcvt (A, 5, PTR );
Printf ("A value = % s", PTR );
PTR = gcvt (B, 6, PTR );
Printf ("B value = % s", 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 <stdlib. h>
Define functions
Double strtodd (const char * nptr, char ** endptr );
Function Description
Strtodd () scans the nptr parameter string and skips the leading space character. Conversion is started only when a number or positive or negative sign is encountered until a non-number or string ends ('') 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 <stdlib. h>
Mian ()
{
Char A [] = "1000000000 ";
Char B [] = "1000000000 ";
Char C [] = "FFFF ";
Printf ("A = % d", strtodd (A, null, 10 ));
Printf ("B = % d", strtodd (B, null, 2 ));
Printf ("C = % d", 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 <stdlib. h>
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, skips the leading space character, and starts conversion only when a number or positive or negative sign is encountered, and then ends when a non-number or string ends ('') end the conversion and return the result. 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 converted long integer. Otherwise, the error code is returned and stored 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 <stdlib. h>
Main ()
{
Char A [] = "1000000000 ";
Char B [] = "1000000000 ";
Char C [] = "FFFF ";
Printf ("A = % d", strtol (A, null, 10 ));
Printf ("B = % d", strtol (B, null, 2 ));
Printf ("C = % d", strtol (C, null, 16 ));
}
Run
A = 1000000000
B = 512
C = 65535.
Strtoul (convert a string to an unsigned long integer)
Related functions
Atof, atoi, atol, strtodd, strtol
Header file
# Include <stdlib. h>
Define functions
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 () scans the nptr parameter string, skips the leading space string, and starts conversion only when a number or positive or negative sign is encountered, and then ends when a non-number or string ends ('') end the conversion and return the result. 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 converted long integer. Otherwise, the error code is returned and stored in errno.
Additional instructions
The conversion string specified by erange exceeds the valid range.
Example
Refer to strtol ()
Toascii (convert an integer to a valid ASCII character)
Related functions
Isascii, toupper, tolower
Header file
# Include <ctype. h>
Define functions
Int toascii (int c)
Function Description
Toascii () converts parameter C to a 7-bit unsigned char value, and the eighth digit is cleared. This character is converted to an ASCII character.
Return Value
Returns the converted ASCII code.
Example
# Include <stdlib. h>
Main ()
{
Int A = 217;
Char B;
Printf ("Before toascii (): A value = % d (% C)", a, );
B = toascii ();
Printf ("after toascii (): A value = % d (% C)", B, B );
}
Run
Before toascii (): A value = 217 ()
After toascii (): A value = 89 (y)
Tolower (converts uppercase letters to lowercase letters)
Related functions
Isalpha, toupper
Header file
# Include <stdlib. h>
Define functions
Int tolower (int c );
Function Description
If the parameter C is an uppercase letter, the corresponding lowercase letter is returned.
Return Value
Returns the converted lowercase letter. If no conversion is required, the C value is returned.
Additional instructions
Example
/* Convert uppercase letters in the S string to lowercase letters */
# Include <ctype. h>
Main ()
{
Char s [] = "abcdefgh12345 ;! # $ ";
Int I;
Printf ("Before tolower (): % s", S );
For (I = 0; I <sizeof (s); I ++)
S = tolower (s );
Printf ("After tolower (): % s", S );
}
Run
Before tolower (): abcdefgh12345 ;! # $
After tolower (): abcdefgh12345 ;! # $
Toupper (converts lowercase letters to uppercase letters)
Related functions
Isalpha, tolower
Header file
# Include <ctype. h> S
Define functions
Int toupper (int c );
Function Description
If the parameter C is a lowercase letter, the uppercase letter of the ing is returned.
Return Value
Returns the converted uppercase letter. If no conversion is required, the C value is returned.
Additional instructions
Example
/* Convert lowercase letters in the S string to uppercase letters */
# Include <ctype. h>
Main ()
{
Char s [] = "abcdefgh12345 ;! # $ ";
Int I;
Printf ("Before toupper (): % s", S );
For (I = 0; I <sizeof (s); I ++)
S = toupper (s );
Printf ("After toupper (): % s", S );
}
Run
Before toupper (): abcdefgh12345 ;! # $
After toupper (): abcdefgh12345 ;! # $