OJ the differences between g++ and C + + in the language options in the submission topic (reprint)

Source: Internet
Author: User

Original link: http://blog.polossk.com/201405/c-plus-plus-g-plus-plus

g++?

First of all, correct a concept, C + + is a computer programming language, g++ is not a language, is a compiler to compile C + + program commands.

So what's the difference between them?

In the language options in the submission topic, g++ and C + + both represent the way to compile. To be precise, choosing C + + means that you will be using the most standard compilation method, which is the ANSI C + + compilation. If you're using g++, it means you'll use the most mundane compilers in the GNU project (in fact, the Code::Blocks's own compiler that we're familiar with, which is typically the MinGW gcc in the Windows environment, In Linux, GCC and the former are basically a thing) to compile. Similar to the choice of C and GCC, the former is the standard C compiler compiled, the latter is also using GCC to compile.

compiler differences-compiler optimizations

Of course, most of the time we have the code in C + + submission passed, but g++ failed? It is well known that different compilers make some different optimizations to the code. Give me one of the simplest examples. For a single statement (note that it is a single statement, not the kind of front + + and after + + that is included in the statement):

12 a: a+ +; b: + +a;

Generally speaking, we all know that the final result of these two statements is the same, that is, a self-increase of 1. However, the gap between the two is still there. If you understand it from the perspective of standard C. a++ This statement is equivalent to

1 A: a = a + 1;

That is, I am calling first, then self-increasing. During the call, a new data address is requested to hold the temporary variable a ', then a ' is added 1, then a ' is assigned to a.

But ++a this statement does not need to be so troublesome. Because he is the first self-increase, after the call, that is, to save the application of new address of Kung Fu. So theoretically, the time consumption is different, if you are using the standard C compilation method, you can find this difference. After all, the time it takes to apply for temporary memory is much longer than making a change to the known memory data.

But the compiler's optimization is reflected in the same results, but time-consuming differences. If you use GCC to compile, you will find that there is basically no difference between the first + + and the Post + +. This is the tip of the iceberg in compiler optimizations. There are, in fact, many areas of optimization.

why did g++ submit WA?

All right, come back to reality. I was doing POJ 3122 this problem yesterday, once again met G++wa;c++ac embarrassing situation.

Why is it? In fact, this is also part of the compiler optimization, which is the accuracy of the default.

As is known to all, a long long type, as a data type that is recognized as a basic data type in c/c++11, has a different type identifier in different environments. That's the%LLD and%i64d we talked about. Similarly, a double type is also an interesting type. The double type is, in fact, precisely the two-precision type, his memory length is generally more than the float type (single-precision) more than one times, sometimes very early in the standard is the double is called long float. So there is the reason why the float type is used with%f,double%lf. But because now is not the kind of memory in the past a few megabytes, more open a double will be super memory age, so double and float in GCC is automatically optimized.

When reading data with scanf, the%LF is used in order to distinguish it from float.

When writing data in printf, because in essence, double and float are the same type, but memory consumption is different, their identifiers are%f, note that this differs from standard C, here are%f.

Of course for another special type long double although not commonly used, but the compiler is still supported, here is an episode, in theory, a long double should be twice times the double (similar to a long long and int relationship, because long and int is actually a thing). But in fact, long double is very strange is a 10-byte monster, he has two free bytes, how the change will not change. The input and output identifiers are%lf, uppercase L.

But there is a problem here, why I use the local%f WA, OJ on the use of%f will be AC?

Because if we use the Code::Blocks IDE under Windows, the compiler is MinGW this thing. In fact, in order to try to keep GCC cross-platform, MinGW in some places is directly using msvc things, and the biggest impact on us is the problem of this identifier. Simply put, if you are going to test in a native, then it is best to use that identifier system of standard C; If you want to commit the code, change it to the GCC identifier system.

Then there is the compiler version of the problem, now the MinGW version has reached 4.8, but Poj still make 4.4, so the lower version of the compiler will also have some unusual problems.

Of course, there is a simpler way, is directly with the input and output stream in the control input and output, it is more convenient, and cross-platform performance is better, there is no such error because of identifiers.

Make a list of what it looks like:

Double F; POJ g++ Submit POJ C + + submission Native Test (MinGW GCC 4.8) The safest way
Input scanf ("%lf", &f); scanf ("%lf", &f); scanf ("%lf", &f); Cin >> F;
Output printf ("%f", f); printf ("%lf", f); printf ("%lf", f); cout << F;

It is probably so much, I hope you avoid this kind of mistake.

OJ the Language options in the submission topic g++ and C + + (reprint)

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.