atof(To convert a string into a floating-point number)
Related Functions atoi,atoll,strtod,strtol,strtoul
Header Files #include <stdlib.h>
Defined Functions double atof(const char * nptr);
Function Description atof()It scans the string parameter nptr,Skip over white space characters,Until a few metWord or the sign began to do the conversion,The encounter ended when non-numeric or string(‘/0’)Before the end of conversion,And the results returned。Parameter may include the positive and negative string nptrNumber、Decimal or E(e)To represent the exponent part,123.456 or 123e-2。
return value Floating-point conversion after the return。
Additional information atof()And use strtod(nptr,(char**)NULL)Same result。
1 2 3 4 5 6 7 8 9 10 11 |
范例 /* 将字符串a 与字符串b转换成数字后相加*/ #include<stdlib.h> main() { char *a=”-100.23”; char *b=”200e-2”; float c; c=atof(a)+atof(b); printf(“c=%.2f/n”,c); } 执行 c=-98.23 |
atoi(Converting the string to integer)
Related Functions atof,atoll,atrtod,strtol,strtoul
Header Files #include<stdlib.h>
Defined Functions int atoi(const char * nptr);
Function Description atoi()It scans the string parameter nptr,Skip over white space characters,Until you meet numbers or the sign began to do the conversion,The encounter ended when non-numeric or string(‘/0’)Before the end of conversion,And the results returned。
return value After the return integer conversion。
Additional information atoi()And use strtol(nptr,(char**)NULL,10);Same result。
1 2 3 4 5 6 7 8 9 10 11 |
范例 /* 将字符串a 与字符串b转换成数字后相加*/ #include<stdlib.h> mian() { char a[]=”-100”; char b[]=”456”; int c; c=atoi(a)+atoi(b); printf(c=%d/n”,c); } 执行 c=356 |
atoll(Converts a string grow integer)
Related Functions atof,atoi,strtod,strtol,strtoul
Header Files #include
Defined Functions long atoll(const char * nptr);
Function Description atoll()It scans the string parameter nptr,Skip over white space characters,Until you meet numbers or the sign began to do the conversion,The encounter ended when non-numeric or string(‘/0’)Before the end of conversion,And the results returned。
return value Long integer returned after conversion。
Additional information atoll()And use strtol(nptr,(char**)NULL,10);Same result。
1 2 3 4 5 6 7 8 9 10 11 |
范例 /*将字符串a与字符串b转换成数字后相加*/ #include<stdlib.h> main() { char a[]=”1000000000”; char b[]=” 234567890”; long c; c=atol(a)+atol(b); printf(“c=%d/n”,c); } 执行 c=1234567890 |
gcvt(To convert floating-point number to a string,Take rounding)
Related Functions ecvt,fcvt,sprintf
Header Files #include
Defined Functions char *gcvt(double number,size_t ndigits,char *buf);
Function Description gcvt()It used to convert parameters into an ASCII string number,Parameters ndigits number of digits displayed。gcvt()And ecvt()And fcvt()Difference is that,gcvt()The string conversion contains a decimal point or the sign。If the conversion is successful,The converted string will be placed on the spatial parameters buf pointer。
return value Returns a pointer to a string,This address is a pointer to buf。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
范例 #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/n”,ptr); ptr=gcvt(b,6,ptr); printf(“b value=%s/n”,ptr); } 执行 a value=123.45 b value=-1234.56 |
strtod(Converting the string to float)
Related Functions atoi,atoll,strtod,strtol,strtoul
Header Files #include
Defined Functions double strtod(const char * nptr,char **endptr);
Function Description strtod()It scans the string parameter nptr,Skip over white space characters,Until you meet numbers or the sign began to do the conversion,When non-numeric or string that appears to end(‘/0’)Before the end of conversion,And the results returned。If endptr not NULL,The character pointer will encounter substandard conditions in nptr terminated by endptr return。Parameter may comprise sign string nptr、Decimal or E(e)To represent the exponent part。123.456 or 123e-2。
return value Floating-point conversion after the return。
Additional information Reference atof()。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
范例 /*将字符串a,b,c 分别采用10,2,16 进制转换成数字*/ #include<stdlib.h> mian() { char a[]=”1000000000”; char b[]=”1000000000”; 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)); } 执行 a=1000000000 b=512 c=65535 |
strtol(Converts a string grow integer)
Related Functions atof,atoi,atoll,strtod,strtoul
Header Files #include
Defined Functions long int strtol(const char * nptr,char **endptr,int base);
Function Description strtol()Nptr string parameter will be converted to long integer parameter according to base。Parameter base ranges from 2 to 36, or 0。Hex mode parameter represents the base employed,The base 10 decimal value 10 is used,If the base is 16 in hexadecimal, etc.。When the base value of 0 is done using 10-ary conversion,But encountered such as’0x 'leading character will do the conversion in hexadecimal。Beginning strtol()It scans the string parameter nptr,Skip over white space characters,Until you meet numbers or the sign began to do the conversion,When it encounters the end of the non-numeric or string(‘/0’)End of conversion,And the results returned。If the parameter is not NULL endptr,The character pointer will encounter substandard conditions and terminated in nptr returned by endptr。
return value Long integer returned after conversion,Otherwise, it returns an error code and ERANGE is stored in errno。
Additional information ERANGE specified conversion string exceeds the legal range。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
范例 /* 将字符串a,b,c 分别采用10,2,16进制转换成数字*/ #include<stdlib.h> 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)); } 执行 a=1000000000 b=512 c=65535 |
strtoul(The string into an unsigned long integer)
Related Functions atof,atoi,atoll,strtod,strtol
Header Files #include
Defined Functions unsigned long int strtoul(const char * nptr,char**endptr,int base);
Function Description strtoul()It will be converted into a parameter string nptr unsigned long integer parameter according base。Parameter base ranges from 2 to 36, or 0。Hex mode parameter represents the base employed,The base 10 decimal value 10 is used,When the base 16 is 16 hexadecimal, etc.。When the base value of 0 is done using 10-ary conversion,But encountered such as’0x 'leading character will do the conversion in hexadecimal。Beginning strtoul()It scans the string parameter nptr,Skip string preceding space,Until you meet numbers or the sign began to do the conversion,When it encounters the end of the non-numeric or string(‘/0’)End of conversion,And the results returned。If the parameter is not NULL endptr,The character pointer will encounter substandard conditions and terminated in nptr returned by endptr。
return value Long integer returned after conversion,Otherwise, it returns an error code and ERANGE is stored in errno。
Additional information ERANGE specified conversion string exceeds the legal range。
Reference example strtol()
toascii(Convert integer into a legal ASCII characters)
Related Functions isascii,toupper,tolower
Header Files #include
Defined Functions int toascii(int c)
Function Description toascii()Parameter c will be converted into 7-bit unsigned char value,Eighth will be cleared,That this character will be converted to ASCII characters。
return value The successful conversion of the ASCII character values return。
1 2 3 4 5 6 7 8 9 10 11 |
范例 #include<stdlib.h> main() { int a=217; char b; printf(“before toascii () : a value =%d(%c)/n”,a,a); b=toascii(a); printf(“after toascii() : a value =%d(%c)/n”,b,b); } 执行 before toascii() : a value =217() after toascii() : a value =89(Y) |
toupper(The lowercase letters to uppercase)
Related Functions Islf,toupper
Header Files #include
Defined Functions int tolower(int c);
Function Description If the parameter c is uppercase letters corresponding to the return of the lower case。
return value After the return to lowercase conversion,If the value of parameter c to be converted will be returned。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
范例 /* 将s字符串内的大写字母转换成小写字母*/ #include<ctype.h> main() { char s[]=”aBcDeFgH12345;!#$”; int i; printf(“before tolower() : %s/n”,s); for(i=0;I<sizeof(s);i++) s[i]=tolower(s[i]); printf(“after tolower() : %s/n”,s); } 执行 before tolower() : aBcDeFgH12345;!#$ after tolower() : abcdefgh12345;!#$ |
tolower(Convert uppercase letters to lowercase)
Related Functions Islf,tolower
Header Files #include
Defined Functions int toupper(int c);
Function Description If the parameter c is the capital letter lowercase enantiomeric return。
return value After the conversion of capital letters returned,If the value of parameter c to be converted will be returned。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
范例 /* 将s字符串内的小写字母转换成大写字母*/ #include main() { char s[]=”aBcDeFgH12345;!#$”; int i; printf(“before toupper() : %s/n”,s); for(i=0;I<sizeof(s);i++) s[i]=toupper(s[i]); printf(“after toupper() : %s/n”,s); } 执行 before toupper() : aBcDeFgH12345;!#$ after toupper() : ABCDEFGH12345;!#$ |
Reference to this articleHere~,Routing just re-publishing content to make it easier to read。
Original article written by LogStudio:R0uter's Blog » Commonly used strings in c-number function
Reproduced Please keep the source and description link:https://www.logcg.com/archives/492.html