Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to output long integer values in C language
How to output long integer values in C language
In C/C++, 64 is an integer, and it is always a data type with no clear specification. In today's mainstream compilers, the support for 64-bit integers is also different in standard and form. Generally speaking, 64-bit integers can be defined in two ways: long long and __int64 (VC also supports _int64), while the output to the standard output method is printf ("%lld ",a), printf ("%i64d", a) and cout.

This paper discusses the support of five commonly used C/C++ compilers for 64-bit integers, namely gcc(mingw32), g++(mingw32), gcc(linux i386), g++(linux i386) and Microsoft Visual C++ 6.0. Unfortunately, there is no combination of definition and output mode, which is compatible with these five compilers. In order to thoroughly understand 64-bit integers of different compilers, I wrote a program to evaluate them, and the results are as follows.

In the above table, the correct expression means that the compilation passed and the operation was completely correct; Error means that the compilation passed, but the running result was wrong; Failure to compile means that the compiler cannot compile at all. Observing the above table, we can find the following points:

Long long definition can be used for gcc/g++, independent of platform, but not for VC6.0.

__int64 is a 64-bit long integer defined by Win32 platform compiler, and cannot be used for Linux.

"%lld" is used for Linux i386 platform compiler, while "%I64d" is used for 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 mode of the last row of the table is a function written by myself. As you can see, its compatibility is better than all other output modes. It is a piece of code like this:

Card Print Processor (abbreviation for card print processor)

void printint64(long long a)

{

If (a<= 100000000)

printf("%d\n ",a);

other

{

printf("%d ",a/ 10000000);

printf("d\n ",a 0000000);

}

}

The essence of this writing is to split the larger 64-bit integer into two 32-bit integers, and then output them in turn, and the lower part should be filled with 0. What is the effect of seemingly stupid writing? I compared it with cout output mode, because both it and cout support cross-platform C++. First of all, the running results of printint64 () and cout (without emptying the buffer) are exactly the same, and there will be no errors. My experiment is to output 1000000 random numbers with them respectively. The actual result is that printint64 () needs 1.5s to run the program, while cout needs 2s. Cout is a little slower, so try to avoid using it when outputting a lot of data.