Preface:
In a 16-bit environment, INT/unsignedint occupies 16 bits, and long/unsigned long occupies 32 bits.
In a 32-bit environment, int occupies 32 bits, unsignedint occupies 16 bits, and long/unsigned long occupies 32 bits.
When to use:
The range of long and Int Is [-2 ^ 31,2 ^ 31), that is,-2147483648 ~ 2147483647, while the unsigned range is [^ 32), that is, 0 ~ 4294967295, so the regular 32-bit integer can only process about 4 billion, when the number is more than 4 billion, it will use 64.
64-bit usage range:
Different compilers have different extensions for 64-bit integers. VC uses _ int64/unsigned _ int64 in the range of [-2 ^ 63,2 ^ 63) and [9223372036854775808 ^ 64), that is ~ 9223372036854775807 and 0 ~ 18446744073709551615 (about 180 billion million ).
Note:
1. Different compilers lead to different declarations using 64-bit;
2. Long/unsigned long is generally a declaration method in Linux, such as: G ++
3. _ int64/unsigned _ int64 is usually a 64-bit declarative method in windows, such as:
4. Add ll to assign values explicitly when assigning values;
5. When the 64-bit and 32-bit mixed operations are performed, the 32-bit integer is implicitly converted to a 64-bit integer.
6. Output printf ("");, Longlong uses % LLD output, __int64 uses % i64d, and u can replace d with unsigned characters.
7. After testing, the compiler generally supports two types of operations, so you don't have to worry too much about how to use them.
// ======================================== Gorgeous separation line ====== ==============================================
# Include <stdio. h>
# Include <stdlib. h>
Intmain (){
Unsignedlong Longa = 412432424000ll;
Unsigned _ int64b = 9223372010954775808ll;
Printf ("% i64u \ n", a); // % LLD cannot be output normally, why? The answer is attached.
Printf ("% i64u", B );
System ("pause ");
Return0;
}
Attached test results:
After the experiment, in vc6, Dev, and codeblocks, the C language can all use _ int64, and the output identifier is % i64d. However, if you add 2 L after the number in vc6, an error is returned. You can add only one or no more numbers. I checked the information, __int64 is for Windows and is supported by VC, GCC, and other compilers. However, in UNIX and Linux, Longlong and % LLD are required. The latter is standard C!
I tried Longlong with % i64d, which can be output correctly, but neither Longlong nor _ int64 with % LLD can be output correctly. So I come to the conclusion that Longlong or ,__ int64 must be used with DD in windows. In UNIX and Linux, the standard C Longlong must be used in combination with % LLD.
Note. (Http://hi.baidu.com/tianxingjianhd/blog/item/c44bec3a221fc1ff14cecbf6.html) int main () {__ int64 I = 10; printf ("% l64", I); cout <Endl; return 0 ;}cout <I; // 64-bit integer of has problems C/C ++
In C/C ++, 64 is an undefined standard data type. In today's mainstream compilers, the support for 64-type integers is also different in different forms. Generally, 64-bit integer types are long and _ int64 (VC also supports _ int64), while printf ("% LLD ", a), printf ("% i64d", A), and cout <.
This article discusses five common C/C ++ compilers that support 64-bit integers. These five compilers are GCC (mingw32), g ++ (mingw32 ), GCC (Linux i386), g ++ (Linux i386), Microsoft Visual C ++ 6.0. Unfortunately,There is no combination of definition and output methods, and these five compilers are compatible at the same time.. To thoroughly understand the 64-bit integer types of different compilers, I wrote a program to evaluate them. The results are shown in the following table.
Variable definition |
Output Mode |
GCC (mingw32) |
G ++ (mingw32) |
GCC (Linux i386) |
G ++ (Linux i386) |
Microsoftvisual C ++ 6.0 |
Long long |
"% LLD" |
Error |
Error |
Correct |
Correct |
Unable to compile |
Long long |
"% I64d" |
Correct |
Correct |
Error |
Error |
Unable to compile |
Int64 |
"LLD" |
Error |
Error |
Unable to compile |
Unable to compile |
Error |
Int64 |
"% I64d" |
Correct |
Correct |
Unable to compile |
Unable to compile |
Correct |
Long long |
Cout |
Non-C ++ |
Correct |
Non-C ++ |
Correct |
Unable to compile |
_ Int64 |
Cout |
Non-C ++ |
Correct |
Non-C ++ |
Unable to compile |
Unable to compile |
Long long |
Printint64 () |
Correct |
Correct |
Correct |
Correct |
Unable to compile |
In the above table, correct indicates that the compilation passes and the operation is completely correct. Error indicates that the compilation passes but the running result is incorrect. Failure to compile means that the compiler cannot complete the compilation at all. Observe the table above and we can find the following points:
- Long long can be defined in gcc/g ++ without platform restrictions, but not in vc6.0.
- _ Int64 is a 64-bit long integer definition method of the Win32 platform compiler. It cannot be used in Linux.
- "% LLD" is used for the Linux i386 platform compiler, and "% i64d" is used for the Win32 platform compiler.
- Cout can only be used for C ++ compilation. In vc6.0, cout does not support 64-bit long integers.
Printint64 () in the output method of the last row in the table is a function written by myself. It can be seen that its compatibility is better than all other output methods. It is a piece of code like this:
void printint64(long long a){ if (a<=100000000) printf("%d\n",a); else { printf("%d",a/100000000); printf("%08d\n",a%100000000); }}
The essence of this writing method is to split the large 64-bit integer into two 32-bit integer types, and then output them in sequence. The low part must be supplemented with 0. What is the effect of seemingly stupid writing? I compared it with the cout output mode because it and cout both support cross-platform C ++. First, the running results of printint64 () and cout (do not clear the buffer) are identical and no error occurs. In my experiment, I used both to output 1000000 random numbers. The actual result is that printint64 () ran the program within 1.5s, and cout took 2 s. Cout is a little slower, so try to avoid using it when outputting a large amount of data.
References: https://www.byvoid.com/blog/c-int64/http://blog.csdn.net/liuqz2009/article/details/6932822http://blog.csdn.net/zhongzhiwei/article/details/8678885http://freesoftman.iteye.com/blog/629970
Different data types: msvc and GCC/g ++