Big Hole! Often overlooked and have to pay attention to small details--%I64,%LLD and cout (reprint)

Source: Internet
Author: User

Original address: http://blog.csdn.net/thunders01/article/details/38879553

Just by the pit, Oi a year to know%i64 and%lld there is a difference (do not be too little), long long really should pay attention to

--------------------------------------------original Bo-------------------------------------------

64 is an integral type of data type that has no deterministic specification in C + +. In today's mainstream compilers, support for the 64 integer is also standard and varies in form. In general, 64-bit integers are defined in long long and __int64 two (VC also supports _int64), while output to standard output is printf ("%lld", a), printf ("%i64d", a), and cout << A three different ways.

This article discusses the support for 64-bit integers in the five common C/s + + compilers, five of which are GCC (mingw32), g++ (MINGW32), GCC (Linux i386), g++ (Linux i386), Microsoft Visual C + + 6.0. Unfortunately, there is no combination of definitions and outputs that are compatible with these five compilers . To get a thorough understanding of the different compilers for 64-bit integers, I wrote the program to evaluate them, resulting 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 That's right That's right Cannot compile
Long Long "%i64d" That's right That's right Error Error Cannot compile
__int64 "LLD" Error Error Cannot compile Cannot compile Error
__int64 "%i64d" That's right That's right Cannot compile Cannot compile That's right
Long Long cout Non-C + + That's right Non-C + + That's right Cannot compile
__int64 cout Non-C + + That's right Non-C + + Cannot compile Cannot compile
Long Long Printint64 () That's right That's right That's right That's right Cannot compile

In the table above, correctly refers to the compilation pass, the operation is completely correct; the error means that the compilation passed, but the result is wrong; Unable to compile means the compiler cannot compile at all. Observing the table above, we can find the following points:

    1. A long long definition can be used for gcc/g++, not platform-limited, but not for VC6.0.
    2. __int64 is the definition of the WIN32 platform compiler 64-bit long integer and cannot be used with Linux.
    3. "%lld" is used for the Linux i386 platform compiler, and "%i64d" is used for WIN32 platform compilers.
    4. Cout can only be used for C + + compilation, in VC6.0, the cout does not support 64-bit long integers.

The last line of output in the table is Printint64 () is a function I write myself, it can be seen that its compatibility is better than all other output methods, it is a code like this:

    1. void Printint64 (long long a)
    2. {
    3. if (a<=100000000)
    4. printf ("%d/n", a);
    5. Else
    6. {
    7. printf ("%d", a/100000000);
    8. printf ("%08d/n", a%100000000);
    9. }
    10. }

The essence of this writing is to split the larger 64-bit integer into two 32-bit integers, then output sequentially, and the low part to be 0. What is the effect of a seemingly stupid writing? I compared it to the cout output mode, because it and cout are all cross-platform supported by C + +. First Printint64 () and cout (not emptying the buffer) run the same result without error. My experiment was to output 1 million random numbers using both, and the actual result was that Printint64 () ran out of the program in 1.5s, and cout needed 2s. cout a little bit slower, so when you're outputting a lot of data, try to avoid it.

ZZ from http://blog.csdn.net/zhlynn/archive/2009/03/28/4032152.aspx

Full solution for 64-bit integers (supplemental plates)

The confusion caused by 64-bit shaping is mainly in two aspects, one is the declaration of the data type, and the other is the input and output.

First, if we write the program on our own machine, the situation is categorized as follows:

(1) in the VC6.0 of win, you should write when declaring the data type.

__int64 A;

Use%i64d when entering and exporting

scanf ("%i64d", &a);
printf ("%i64d", a);

(2) in Linux gcc/g++, data type declaration writing

Long Long A;

Input/output time with%LLD

(3) In other IDE under win [including high version of Visual Studio], data type declaration can be used both

%I64D for input and output

================== below can be ignored =========================

The following is an explanation of this confusion, such as the absence of interest to skip

The first thing to say is that, unlike Java and other languages, C + + itself does not specify the number of bits for each data type, only a size relationship is defined, that is, from the occupied bit number, short <= int <= long <= long. As for which type to occupy the number of bits, it is determined by the compiler of the development platform you are using. On the current PC, the usual standard is that int and long are 32-bit, long long is 64 bits. However, if you switch to another platform (such as arm), this number may be different, and the size of the type can be viewed with the sizeof () operator.

Long Long is the newly introduced data type in the C99 standard, and there is no such type in the old VC6.0, so a compile error will occur in VC6.0 with a "long long". To represent a 64-bit integer, VC6 uses a data type created by Microsoft itself, called __int64, so if you are compiling under VC6.0, you should define a 64-bit integer with __int64. The new version of Visual Studio already supports long long. GCC is a long long, we use in the win system of other Ides such as Dev-cpp, code::blocks, etc. are mostly used in the MINGW compilation environment, it is compatible with GCC, so also support long long (in addition, in order to be compatible with MS, also support _ _int64). If you are under pure Linux, you can only use long long.

There's a much more embarrassing situation here about using printf's input and output. In fact, just remember, the main difference is the operating system: if in the win system, then no matter what compiler, all use%I64D, if the Linux system, all use%LLD. This is due to the fact that Ms provides the Msvcrt.dll library with the%I64D approach, although the dev-cpp and other syntax support standards, but also have to use the DLL library provided by MS to complete IO, so this is the case.

==================== ignored this ===========================

So for Acmer, the most concern is in each OJ turn over the topic should be used respectively which way. In fact, there are only a limited number of ways:

If the server is a Linux system, then define a long long,io with%LLD
If the server is a win system, the declaration depends on the compiler:
+ If using MS Series compiler, declare with __int64 [now the new version of Visual Studio also supports long long]
+ If using MINGW environment, declare with a long long
+ No matter what compiler, IO is%i64d

The following list of the major OJ are as follows:

1. Toj:linux System
2. Zoj:linux System
3. Poj:win system, language, such as the choice of C + +, the MS compiler [support two kinds of declarations], such as select gcc/g++, then MinGW
4. Uva:linux System
5. Ural:win system, MS Compiler [supports two types of declarations]
6. Spoj:linux System
7. Sgu:win system, MS Compiler [supports two types of declarations]

If you have a less clear case, you can look at the FAQ on each OJ, which is usually explained.

In addition, in order to avoid confusion, when the amount of data is small, with CIN, cout input and output is also a choice

Big Hole! Often overlooked and have to pay attention to small details--%I64,%LLD and cout (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.