以下程式示範函數 atoi() 對不同字串的轉換結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> #include <stdlib.h> int main( void ) { printf ( "%d\n" , atoi ( "54321" )); printf ( "%d\n" , atoi ( "54.321" )); printf ( "%d\n" , atoi ( "$54321" )); printf ( "%d\n" , atoi ( "$54.321" )); printf ( "%d\n" , atoi ( "54.321%" )); printf ( "%d\n" , atoi ( "5.4321+33=?" )); return 0; } /* 《程式語言教學誌》的範例程式 檔名:catoi.c 功能:示範 stdlib.h 中函數 atoi() 的使用 作者:張凱慶 時間:西元2010年6月 */ |
編譯後執行,結果如下

您可以繼續參考
通用工具 stdlib.h
- double atof(const char *);
- int atoi(const char *);
- double strtod(const char *s, char **r);
- long strtol(const char *s, char **r, int base);
- void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp)(const void *keyval, const void *datum));
- void qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *));
- void *calloc(size_t nobj, size_t size);
- void *malloc(size_t size);
- void *realloc(void *ptr, size_t size);
- void free(void *ptr);
- void abort(void);
- void exit(int);
- int system(const char *s);
- int rand(void);
- void srand(unsigned int seed)
1 則留言:
printf("%d\n", atoi("-3bbbb"));
結果會是-3
printf("%d\n", atoi("bbbb-3bbbb"));
結果卻是0
張貼留言