首页 > 其他分享 >C语言学习:字符串与其他数值类型的转换

C语言学习:字符串与其他数值类型的转换

时间:2023-02-11 21:11:06浏览次数:32  
标签:1234 end start DOUBLE 数值 C语言 PRINT 字符串 atof

 

 1 #include <io_utils.h>
 2 #include <stdlib.h>
 3 #include <errno.h>
 4 
 5 int main() {
 6 //  PRINT_INT(atoi("1234")); // 1234
 7 //  PRINT_INT(atoi("-1234")); // -1234
 8 //  PRINT_INT(atoi("   1234abcd")); // 1234
 9 //  PRINT_INT(atoi("0x10")); // 0
10 //
11 //  PRINT_DOUBLE(atof("12.34")); // 12.34
12 //  PRINT_DOUBLE(atof("-12e34")); // -1.2e+35
13 //  PRINT_DOUBLE(atof("   1.234abcd")); // 1.234
14 //  PRINT_DOUBLE(atof("0x10")); // 16
15 //  PRINT_DOUBLE(atof("0x10p3.9")); // 128
16 
17   char const *const kInput = "1 200000000000000000000000000000 3 -4 5abcd bye";
18   PRINTLNF("Parse: %s", kInput);
19 
20   char const *start = kInput;
21   char *end;
22 
23   while (1) {
24     errno = 0;
25 
26     const long i = strtol(start, &end, 10);
27 
28     if (start == end) {
29       break;
30     }
31 
32     printf("'%.*s'\t ==> %ld.", (int)(end - start), start, i);
33 
34     if (errno == ERANGE) {
35       perror("");
36     }
37 
38     putchar('\n');
39     start = end;
40   }
41 
42   PRINTLNF("Left: %s", end);
43   return 0;
44 }
View Code

 

 

 

标签:1234,end,start,DOUBLE,数值,C语言,PRINT,字符串,atof
From: https://www.cnblogs.com/liumy/p/17112565.html

相关文章