首页 > 代码库 > 整形_长整型的使用范围和规则
整形_长整型的使用范围和规则
整形_长整型的使用范围(两个数的乘积)
1.
I.unsigned long :%lu ; unsigned long long :%llu。
II.当结果在输出格式范围之内,则输出正确,否则不正确。
III. 当结果在输出格式范围之外,则输出的值与结果模(变量类型的大小的值)相等,且输出的值在变量类型的范围内。这里若结果大于2^64,则上述结论不正确。
Code:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 /** 7 long -2147483648~2147483647 2.1*10^9(2^31) 8 unsigned long 0~4294967295 4.2*10^9(2^32) 9 ( (unsigned) int 范围与 (unsigned) long 相同) 10 long long:-9223372036854775808~9223372036854775807 9.2*10^18(2^63) 11 unsigned long long:0~18446744073709551615 1.8*10^19(2^64) 12 ( (unsigned) __int64 范围与 (unsigned) long long 相同) 13 **/ 14 15 printf("%ld\n",2000000000);//<=2147483647,Y 16 ///3000000000 = -1294967296 (mod 2^32) 17 ///而 -1294967296 in [-2147483648,2147483647] 18 printf("%ld\n",3000000000);//>2147483647,N 19 20 printf("%lu\n",4000000000);//<=4294967295,Y 21 ///5000000000 = 705032704 (mod 2^32) 22 ///而 705032704 in [0,4294967295] 23 printf("%lu\n",5000000000);//>4294967295,N 24 25 printf("%lld\n",9000000000000000000);//<=9223372036854775807,Y 26 ///10000000000000000000 = -8446744073709551616 (mod 2^64) 27 ///而 -8446744073709551616 in [-9223372036854775808,9223372036854775807] 28 printf("%lld\n",10000000000000000000);//>9223372036854775807,N 29 30 printf("%llu\n",18446744073709551615);//<=18446744073709551615,Y 31 ///18446744073709551616 <> 18446744069414584320 (mod 2^64) 32 ///超出整形范围(2^64),则发生错误 33 printf("%llu\n",18446744073709551616);//>18446744073709551615,N 34 printf("%llu\n",18446744073709551617);//>18446744073709551615,N 35 printf("%llu\n",-1);//<-18446744073709551616,Y 36 printf("%llu\n",-2);//<-18446744073709551616,Y 37 ///mystery 38 39 return 0; 40 }
2.
///1.一个整数在什么范围内,就默认为什么类型的数
///以下执行优先原则:
///I.在[-2^31,2^31-1]:long II.在[0,2^32-1]:unsigned long
///III.在[-2^63,2^63-1]:long IV.在[0,2^64-1]:unsigned long
///2.若计算式中的所有类型都小于输出的类型
///如计算式的类型是long/unsigned long , 输出的类型为long long/unsigned long long,
///,则输出结果错误!
///3.若计算式中存在数,数的类型大于输出的类型,
///而结果在输出的类型内,则输出结果正确!
///4.运算/输出需要什么类型,就必须转换为相对应的类型
///printf("%ld\n",(long)((double)2147483650*0.1));
Code:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 7 ///1.一个整数在什么范围内,就默认为什么类型的数 8 ///以下执行优先原则: 9 ///I.在[-2^31,2^31-1]:long II.在[0,2^32-1]:unsigned long 10 ///III.在[-2^63,2^63-1]:long IV.在[0,2^64-1]:unsigned long 11 ///2.若计算式中的所有类型都小于输出的类型 12 ///如计算式的类型是long/unsigned long , 输出的类型为long long/unsigned long long, 13 ///,则输出结果错误! 14 ///3.若计算式中存在数,数的类型大于输出的类型, 15 ///而结果在输出的类型内,则输出结果正确! 16 ///4.运算/输出需要什么类型,就必须转换为相对应的类型 17 ///printf("%ld\n",(long)((double)2147483650*0.1)); 18 19 20 ///2 21 ///long operation(+ - * /) long = long 22 printf("%lld\n",1+1); 23 printf("%lld\n",1-1); 24 printf("%lld\n",1*1); 25 printf("%lld\n",1/1); 26 27 printf("\n"); 28 29 ///long operation (+ - * /) long = long long 30 printf("%lld\n",2000000000+2000000000); 31 printf("%lld\n",2000000000-(-2000000000)); 32 printf("%lld\n",2000000000*2000000000); 33 34 printf("\n"); 35 36 ///unsigned long operation (+ - * /) unsigned long = long long 37 printf("%lld\n",4000000000+4000000000); 38 printf("%lld\n",4000000000-(-4000000000)); 39 printf("%lld\n",4000000000*(-4000000000)); 40 41 printf("\n"); 42 43 ///long operation (+ - * /) unsigned long = long long 44 printf("%lld\n",2000000000+4000000000); 45 printf("%lld\n",4000000000+2000000000); 46 printf("%lld\n",2000000000-4000000000); 47 printf("%lld\n",4000000000-2000000000); 48 printf("%lld\n",2000000000*4000000000); 49 printf("%lld\n",4000000000*2000000000); 50 51 printf("\n"); 52 53 ///long operation (+ - * /) long long = long long 54 printf("%lld\n",8000000000+2000000000); 55 printf("%lld\n",2000000000+8000000000); 56 printf("%lld\n",8000000000-2000000000); 57 printf("%lld\n",2000000000-8000000000); 58 printf("%lld\n",8000000000*1000000000); 59 printf("%lld\n",1000000000*8000000000+1); 60 printf("%lld\n",1+1000000000*8000000000); 61 62 printf("\n"); 63 64 ///unsigned long operation (+ - * /) long long = long long 65 printf("%lld\n",8000000000+4000000000); 66 printf("%lld\n",4000000000+8000000000); 67 printf("%lld\n",8000000000-4000000000); 68 printf("%lld\n",4000000000-8000000000); 69 ///乘会超界 70 71 printf("\n"); 72 73 printf("\n--------------------------\n"); 74 75 ///long operation(+ - * /) long = long 76 long a=1; 77 printf("%lld\n",a+a); 78 printf("%lld\n",a-a); 79 printf("%lld\n",a*a); 80 printf("%lld\n",a/a); 81 82 printf("\n"); 83 84 ///long operation (+ - * /) long = long long 85 long b=2000000000; 86 printf("%lld\n",b+b); 87 printf("%lld\n",b-(-b)); 88 printf("%lld\n",b*b); 89 90 printf("\n"); 91 92 ///unsigned long operation (+ - * /) unsigned long = long long 93 unsigned long c=4000000000; 94 printf("%lld\n",c+c); 95 printf("%lld\n",c-(-c)); 96 printf("%lld\n",c*(-c)); 97 98 printf("\n"); 99 100 ///long operation (+ - * /) unsigned long = long long 101 printf("%lld\n",b+c); 102 printf("%lld\n",c+b); 103 printf("%lld\n",b-c); 104 printf("%lld\n",c-b); 105 printf("%lld\n",b*c); 106 printf("%lld\n",c*b); 107 108 printf("\n"); 109 110 ///long operation (+ - * /) long long = long long 111 long long d=8000000000; 112 long e=1000000000; 113 printf("%lld\n",d+b); 114 printf("%lld\n",b+d); 115 printf("%lld\n",d-b); 116 printf("%lld\n",b-d); 117 printf("%lld\n",d*e); 118 printf("%lld\n",e*d+1); 119 printf("%lld\n",1+e*d); 120 121 printf("\n"); 122 123 ///unsigned long operation (+ - * /) long long = long long 124 printf("%lld\n",d+c); 125 printf("%lld\n",c+d); 126 printf("%lld\n",d-c); 127 printf("%lld\n",c-d); 128 ///乘会超界 129 130 printf("\n"); 131 132 printf("\n--------------------------\n"); 133 134 ///3 135 printf("%ld\n",6000000000+(-4000000000)); 136 printf("%ld\n",100000000001-100000000000); 137 138 printf("\n"); 139 140 printf("\n--------------------------\n"); 141 142 ///4 143 printf("%ld\n",5.5); 144 145 printf("\n"); 146 147 printf("%ld\n",2147483650*0.1); 148 printf("%ld\n",2147483651*0.1); 149 printf("%ld\n",20*0.1); 150 printf("%ld\n",21*0.1); 151 152 printf("\n"); 153 154 printf("%ld\n",(double)2147483650*0.1); 155 printf("%ld\n",(double)2147483651*0.1); 156 printf("%ld\n",(double)20*0.1); 157 printf("%ld\n",(double)21*0.1); 158 159 printf("\n"); 160 161 printf("%ld\n",(long)((double)2147483650*0.1)); 162 printf("%ld\n",(long)((double)2147483651*0.1)); 163 printf("%ld\n",(long)((double)20*0.1)); 164 printf("%ld\n",(long)((double)21*0.1)); 165 166 167 printf("\n"); 168 169 170 171 return 0; 172 } 173
整形_长整型的使用范围和规则
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。