Atof (converting strings to floating-point numbers)
Header file # include
Define the function doubleatof (constchar * nptr);
functional description
Atof () scans the parameter nptr string, skips the leading space character, and does not start conversion until it encounters numbers or positive and negative symbols, and does not bundle conversion until it encounters non-numbers or the end of the string (''), and returns the result. The parameter nptr string can contain symbols, decimal points or E(e) to represent the exponential part, such as 123.456 or 123e-2.
Return Value Returns the converted floating-point number.
Explaining atof () is the same as using strtod(nptr, (char**)NULL).
Example/* Convert string A and string B into numbers and add them */
# include & ltstdlib.h & gt
Master ()
{
char * a = "- 100.23 ";
char * b = " 200e-2 ";
Floating c;
c = atof(a)+atof(b);
printf("c=%.2f\n ",c);
}
carry out
c=-98.23
////////////////////////////////////////////////////////////////////
Atoi (converting a string into an integer)
Header file # include
Define the function intaoi (constchar * nptr);
Function description: atoi () scans the parameter nptr string, skips the leading space character, and does not start the conversion until it encounters numbers or positive and negative symbols, and ends the conversion when it encounters non-numbers or the end of the string ('), and returns the result.
Return Value Returns the converted integer.
Additional notes on atoi () and using strtol(nptr, (char**)NULL, 10); The result is the same.
Example/* Convert string A and string B into numbers and add them */
# include & ltstdlib.h & gt
Encourage ()
{
char a[]= "- 100 ";
char b[]= " 456 ";
int c;
c = atoi(a)+atoi(b);
printf(c=%d\n ",c);
}
carry out
c=356
//////////////////////////////////////////////////////////////////////////
Atol (converting a string into an integer)
Header file # include
Define the function longatol (constchar * nptr);
Function description: atol () scans the parameter nptr string, skips the leading space character, and does not start the conversion until it encounters numbers or positive and negative symbols, and ends the conversion when it encounters non-numbers or the end of the string (''), and returns the result.
Return Value Returns a converted long integer.
Atol () and the supplementary explanation of using STRTOL (nptr, (char * *) null, 10); The result is the same.
Example/* Convert string A and string B into numbers and add them */
# include & ltstdlib.h & gt
Master ()
{
char a[]= " 100000000 ";
char b[]= " 234567890 ";
Long c;
c = atol(a)+atol(b);
printf("c=%d\n ",c);
}
carry out
c= 1234567890
////////////////////////////////////////////////////////////////
Gcvt (Converts floating-point numbers to strings and rounds them off)
Correlation functions ecvt, fcvt, sprintf
Header file # include
Define the function char *gcvt(double number, size_t ndigits, char * buf);
The function description gcvt () is used to convert the parameter number into an ASCII string, and the parameter ndigits represents the number of displayed digits. The difference between Gcvt () and ecvt () and fcvt () is that the string converted by gcvt () contains decimal points or positive and negative symbols. If the conversion is successful, the converted string will be put into the space pointed by the parameter buf pointer.
The return value returns a string pointer, and this address is the buf pointer.
Examples of additional explanations # include
Master ()
{
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);
}
carry out
A value = 123.45
B value =- 1234.56
///////////////////////////////////////////////////////////////////////
Strtod (Converts a string to a floating-point number)
Header file # include
Define the function doublestrod (constchar * nptr, char * * endptr);
Function description: strtod () scans the parameter nptr string, skips the leading space character, and does not start the conversion until it encounters numbers or positive and negative symbols, and does not end the conversion until there is a non-number or the end of the string ('), and returns the result. If endptr is not empty, endptr will return the character pointer in the nptr terminated due to unqualified conditions. The parameter nptr string can contain symbols, decimal points or E(e) to represent the exponential part. For example, 123.456 or 123e-2.
Return Value Returns the converted floating-point number.
Please refer to atof () for other instructions.
Example/* Convert the strings A, B and C into numbers in 10, 2 and 16 respectively */
# include & ltstdlib.h & gt
Encourage ()
{
char a[]= " 100000000 ";
char b[]= " 100000000 ";
char c[]= " ffff ";
printf("a=%d\n ",strtod(a,NULL, 10));
printf("b=%d\n ",strtod(b,NULL,2));
printf("c=%d\n ",strtod(c,NULL, 16));
}
carry out
a= 100000000
b=5 12
c=65535
////////////////////////////////////////////////////////////////////////////////////
Strtol (Converts a string to an integer)
Correlation functions atof, atoi, atol, strtod, strtoul
Header file # include
Define the function long int strtol (constchar * nptr, char * * endptr, intbase);
functional description
Strtol () converts the parameter nptr string into a long integer according to the parameter base. Parameter cardinality ranges from 2 to 36, that is, 0. The parameter base indicates the basic system adopted. For example, if the base value is 10, the base system is 10, and if the base value is 16, the base system is 16. When the base value is 0, it is converted with 10, but when a leading character such as "0x" is encountered, it is converted with 16. At first, strtol () will scan the parameter nptr string, skip the leading space character, start the conversion until it encounters a number or a sign, and then end the conversion when it encounters a non-number or the end of the string ('), and return the result. If the parameter endptr is not empty, the character pointer in nptr terminated due to unqualified conditions will be returned by endptr.
The return value returns the converted long integer, otherwise it returns ERANGE and stores the error code in errno.
Additional note The conversion string specified by ERANGE is out of legal range.
Example/* Convert the strings A, B and C into numbers in 10, 2 and 16 respectively */
# include & ltstdlib.h & gt
Master ()
{
char a[]= " 100000000 ";
char b[]= " 100000000 ";
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));
}
carry out
a= 100000000
b=5 12
c=65535
///////////////////////////////////////////////////////////
Strtoul (Converts a string to an unsigned long integer)
Correlation functions atof, atoi, atol, strtod, strtol.
Header file # include
Define the function unsigned long int strool (constchar * nptr, char * * endptr, intbase);
functional description
Strtoul () converts the parameter nptr string into an unsigned long integer according to the parameter base. Parameter cardinality ranges from 2 to 36, that is, 0. The parameter base indicates the basic system adopted. For example, if the base value is 10, the base system is 10, and if the base value is 16, the base system is 16. When the base value is 0, it is converted with 10, but when a leading character such as "0x" is encountered, it is converted with 16. Strtoul () first scans the parameter nptr string, skips the preceding space string, and starts the conversion until it encounters numbers or positive and negative symbols, then ends the conversion when it encounters non-numbers or the end of the string (""), and returns the result. If the parameter endptr is not empty, the character pointer in nptr terminated due to unqualified conditions will be returned by endptr.
Return value
Returns the converted long integer, otherwise returns ERANGE and stores the error code in errno.
additional information
The conversion string specified by ERANGE is out of legal range.
model
Please refer to strtol ()
ToASCII (converting integers into legal ASCII characters)
correlation function
Isasi, Tupper, Tolois
header file
# include & ltctype.h & gt
Define function
International Trade Commission (ITC)
functional description
Toascii () will convert the parameter c into an unsigned char value of 7 bits, and when the eighth bit is cleared, this character will be converted into an ascii code character.
Return value
Returns the converted ASCII character value.
model
# include & ltstdlib.h & gt
Master ()
{
int a = 2 17;
char b;
printf(" before toas CII():a value = % d(% c)\ n ",a,a);
b = toa scii(a);
printf(" after toas CII():a value = % d(% c)\ n ",b,b);
}
carry out
Before toascii (): a value =2 17 ()
After toascii (): a value =89(Y)
////////////////////////
Tolower (convert uppercase letters to lowercase letters)
The correlation function is Alpha Tupper.
Header file # include
Define the function int to lower (int c);
Function Description: If the parameter c is uppercase, the corresponding lowercase letter will be returned.
The return value returns the converted lowercase letters, and if conversion is not needed, the parameter c value is returned.
Additional explanation example/* Convert uppercase letters in S string to lowercase letters */
# include & ltctype.h & gt
Master ()
{
char s[]= " abcdefgh 12345; ! #$";
int I;
Printf ("before tolower (): %s\n", s);
for(I = 0; I< sizeof (s); i++)
s = to lower(s);
printf("after tolower() : %s\n ",s);
}
carry out
Before tolower (): abcdefgh12345; ! #$
After tolower (): abcdefgh12345; ! #$
Toupper (converts lowercase letters into uppercase letters)
The correlation function is α, tolower
Header file # include
Define the function int toupper (int c);
Function description: If parameter c is lowercase, the corresponding uppercase letter is returned.
The return value returns the converted uppercase letters, and if conversion is not needed, the parameter c value is returned.
Example/* Converts lowercase letters in an S string to uppercase letters */
# include & ltctype.h & gt
Master ()
{
char s[]= " abcdefgh 12345; ! #$";
int I;
printf("before toupper() : %s\n ",s);
for(I = 0; I< sizeof (s); i++)
s = toupper(s);
printf("after toupper() : %s\n ",s);
}
carry out
Before toupper (): abcdefgh12345; ! #$
After toupper (): abcdefgh12345; ! #$
//I wish you success