Microsoft Visual C ++ does not support long and visuallong

Source: Internet
Author: User

Microsoft Visual C ++ does not support long and visuallong

Microsoft Visual C ++ does not support long

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 Printf output mode Gcc (mingw32) G ++ (mingw32) Gcc (linux i386) G ++ (linux i386) MicrosoftVisual C ++ 6.0
Long "% Lld" Error Error Correct Correct Unable to compile
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 Cout Non-C ++ Correct Non-C ++ Correct Unable to compile
_ Int64 Cout Non-C ++ Correct Non-C ++ Unable to compile Unable to compile
Long Printint64 () Correct Correct Correct Correct Unable to compile

Note that the blue part I marked must be clearly distinguished between L and I.

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:

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.

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

64-bit integer full solution (Supplemental Board)

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

First, if we write programs on our own machine, the situation is classified as follows:

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

_ Int64;

% I64d is used for input and output.

Scanf ("% I64d", & );
Printf ("% I64d", );

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

Long;

% Lld is used for input and output.

(3) In other ides under win, [including Visual Studio of higher versions], the data type declaration can be either of the above two types.

% I64d for Input and Output

============================ ======

The following is an explanation of this confusion. skip this step if you are not interested.

The first thing to note is that, unlike Java and other languages, C/C ++ does not specify the number of digits for each data type, but only limits the size relationship, that is to say, the short <= int <= long. The number of characters occupied by the specific type is determined by the compiler of your development platform. On the current PC, the general standard is that int and long are both 32-bit, and long is 64-bit. However, if you switch to another platform (such as ARM), the number may be different. You can use the sizeof () operator to view the size of the type.

Long is a new data type introduced in the C99 standard, which is not available in the ancient VC6.0. Therefore, using "long" in VC6.0 will cause a compilation error. To represent a 64-bit integer, VC6 uses a data type developed by Microsoft, called _ int64. Therefore, if you compile the data in VC6.0, _ int64 should be used to define a 64-bit integer. The New Visual Studio version supports long. GCC supports long. Most of the other ides we use in the win system, such as Dev-Cpp and Code: Blocks, use the MinGW compiling environment, which is compatible with GCC, therefore, long is also supported (and _ int64 is also supported for compatibility with MS ). In a pure linux environment, you can only use long.

For the Input and Output Using printf, there is a more progressive situation. In fact, we only need to remember that the main difference lies in the operating system: if the windows system is used, % I64d will be used regardless of the compiler; if the linux system is used, % lld will be used. This is because MS provides msvcrt. the dll library uses the % I64d method. Although Dev-Cpp and other syntaxes support standards, it also has to use the dll library provided by MS to complete IO, this is the case.

========================================================= ==========

For ACMer, the most important thing is the method used to submit questions in each OJ. In fact, there are only a few methods:

If the server is a linux system, long is defined, and % lld is used for IO.
If the server is a win system, the Declaration depends on the compiler:
+ If you use the MS Series compiler, declare the use of _ int64 [now the new version of Visual Studio supports long]
+ If the MinGW environment is used, the long
+ Regardless of the compiler, I/O always % I64d

The following lists the major OJ scenarios:

1. TOJ: Linux System
2. ZOJ: Linux System
3. POJ: Win system. For example, if C/C ++ is selected, the MS compiler [supports two declarations] is used. If GCC/G ++ is selected, the MinGW is used.
4. Ultraviolet A: Linux System
5. Ural: Win system, MS compiler [two declarations are supported]
6. SPOJ: Linux System
7. SGU: Win system, MS compiler [two declarations are supported]

If you are not clear about the situation, you can first look at the FAQ on each OJ, usually there are instructions.

In addition, to avoid confusion, the use of cin and cout for input and output is also an option when the data volume is small.

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.