atof() 將字符串轉換為雙精度浮點型值 atoi() 將字符串轉換為整型值 atol() 將字符串轉換為長整型值 strtod() 將字符串轉換為雙精度浮點型值,并報告不能被轉換的所有剩余數字 strtol() 將字符串轉換為長整值,并報告不能被轉換的所有剩余數字 strtoul() 將字符串轉換為無符號長整型值,并報告不能被轉換的所有剩余數字
/* strtod example */#include <stdio.h> /* PRintf, NULL */#include <stdlib.h> /* strtod */int main (){ char szOrbits[] = "365.24 29.53"; char* pEnd; double d1, d2; d1 = strtod (szOrbits, &pEnd); d2 = strtod (pEnd, NULL); printf ("The moon completes %.2f orbits per Earth year./n", d1/d2); return 0;}/* strtol example */#include <stdio.h> /* printf */#include <stdlib.h> /* strtol */int main (){ char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff"; char * pEnd; long int li1, li2, li3, li4; li1 = strtol (szNumbers,&pEnd,10); li2 = strtol (pEnd,&pEnd,16); li3 = strtol (pEnd,&pEnd,2); li4 = strtol (pEnd,NULL,0); printf ("The decimal equivalents are: %ld, %ld, %ld and %ld./n", li1, li2, li3, li4); return 0;}/* strtoul example */#include <stdio.h> /* printf, NULL */#include <stdlib.h> /* strtoul */int main (){ char buffer [256]; unsigned long ul; printf ("Enter an unsigned number: "); fgets (buffer, 256, stdin); ul = strtoul (buffer, NULL, 0); printf ("Value entered: %lu. Its double: %lu/n",ul,ul*2); return 0;}新聞熱點
疑難解答