1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
// 字符串转数字等 #include <stdio.h> // atoi()字符串转INT atof()字符串转浮点 atol()字符转LONG型 #include <stdlib.h> int main(int argc, char const *argv[]) { char a[11] = "abcdef"; char c[11] = "123"; int b, d; b = atoi(a); d = atoi(c); // 输出0 123 printf("%d %d\n", b, d); getchar(); return 0; } // strtol() strtoul()无符号long strtod()等是atoxxx的复杂版本 #include <stdio.h> #include <stdlib.h> int main(int argc, char const *argv[]) { char number[30]; char * end; long value; puts("Enter a number (empty line to quit):"); while(gets(number) && number[0] != '\0') { value = strtol(number, &end, 10); printf("value:%ld stopped at %s (%d)\n", value, end, *end); value = strtol(number, &end, 2); printf("value:%ld stopped at %s (%d)\n", value, end, *end); puts("next number:"); } puts("Bye!"); /* Enter a number (empty line to quit): 10 //输入10 当基数(进制)为10时value=10 为2时 value=2 end为空 *end为0 value:10 stopped at (0) value:2 stopped at (0) next number: 10a //输入10a end为a *end为97(ANSI编码) value:10 stopped at a (97) value:2 stopped at a (97) */ getchar(); return 0; } // gcc -std=c99 支持C99特性 #include <stdio.h> int main(int argc, char const *argv[]) { // for中直接定义i 需要C99支持 for (int i = 0; i < 3; ++i) { printf("%d\n", i); } getchar(); return 0; } // 存储类、链接、内存管理 // auto extern static register const volatile restricted // rand() srand() time() malloc() calloc() free() #include <stdio.h> int outFileCanRead = 13; //文件作用域,外部链接external linkage // 这里的static表明链接类型,并非存储时期。所有的文件作用域变量都具有静态存储时期 static int thisFileCanRead = 13; //文件作用域,内部链接internal linkage int main(int argc, char const *argv[]) { static int notAuto = 13; //静态存储时期 int mainCanRead = 13; //代码段作用域,空链接no linkage 自动存储时期 //函数作用域 function scope go to语句所使用的标签,空链接 //C使用作用域、链接和存储时期来定义5种存储类 /* 存储类 时期 作用域 链接 声明方式 自动 自动 代码块 空 代码块内 寄存器 自动 代码块 空 代码块内,使用关键字register 具有外部链接的静态 静态 文件 外部 所有函数之外 具有内部链接的静态 静态 文件 内部 所有函数之外,使用关键字static 空链接的静态 静态 代码块 空 代码块内,使用关键字static */ printf("%d %d\n", notAuto, mainCanRead); getchar(); return 0; } |