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 |
// 寄存器变量 #include <stdio.h> void macho(register int n); //允许 int wontwork(static int n); //不允许 int main(int argc, char const *argv[]) { // 增加register关键字,"如果幸运" 变量就会被存放在CPU的寄存器中或速度最快的可用内存中 // 寄存器变量能比普通变量更快的访问和操作,但是不能获取其地址 // "如果幸运":声明寄存器变量是一个请求,而非一条直接的命令。编译器会根据你的请求和可用寄存器个数或 // 可用高速内存的数量进行权衡,有可能会声明失败(仍然是普通变量,但是依然不能对它使用地址运算符) // 使用register声明的类型是有限的。例如:处理器可能没有足够大的寄存器来容纳double类型 register int quick; return 0; } // extern关键字 具有外部链接的静态变量 #include <stdio.h> int errupt; //外部定义的变量 double up[100]; //外部定义的数组 extern char coal; //如果coal是在其他文件中定义的变量,必须使用extern关键字再次声明 int main(int argc, char const *argv[]) { extern int errupt; //可选的声明,只是表面main函数使用了这个变量 extern double up[]; //可选的声明 extern int errupt = 13; //错误,再次声明只是一个引用声明,而非定义声明 int errupt; //如果这样声明,main函数中会新建一个名为errupt的独立的自动变量 return 0; } // 变量的默认值 #include <stdio.h> int test; int main(int argc, char const *argv[]) { int test1; static int test2; register int test3; // 静态变量会默认为0,非静态变量会取得垃圾值 printf("%d\n%d\n%d\n%d", test, test1,test2, test3); getchar(); return 0; } // 函数也具有存储类 #include <stdio.h> double ganmma(); //默认为外部的 static double beta(); //内部,外部文件可以使用同名的不同函数。 extern double delta(); //外部 int main(int argc, char const *argv[]) { return 0; } // 随机数 #include <stdio.h> #include <stdlib.h> #include <time.h> int mt_rand(int, int); int main(int argc, char const *argv[]) { int i = mt_rand(0,5); printf("%d\n", i); } int mt_rand(int begin, int end) { // 以当前时间戳为随机种子 srand(time(0)); return rand() % (end - begin + 1) + begin; } |