C/C ++ 64-bit integer

Source: Internet
Author: User
Tags integer definition

From http://www.byvoid.com/blog/c-int64/

C/C ++ 64-bit integer

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:

  1. Long long can be defined in gcc/g ++ without platform restrictions, but not in vc6.0.
  2. _ Int64 is a 64-bit long integer definition method of the Win32 platform compiler. It cannot be used in Linux.
  3. "% LLD" is used for the Linux i386 platform compiler, and "% i64d" is used for the Win32 platform compiler.
  4. 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.

Related Article

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.