C C's strengthening

Source: Internet
Author: User
Tags modifiers

Tag: equals variable Request Const parameter nbsp Three mesh operator and accept

1.register keyword Enhancement

The register modifier implies that the corresponding variable of the compiler will be used frequently and, if possible, should be kept in the CPU register to speed up its storage speed, which is only a request that the compiler can reject.

(1) The register variable must be a type that can be accepted by the CPU.

This usually means that the register variable must be a single value, and the length should be less than or equal to the length of the integral type. However, some machine registers can also store floating-point numbers. The number of bits in the register is usually the same as the number of CPUs. Now the CPU is mostly 32bit and 64bit.

(2) because the register variable may not be stored in memory, "&" cannot be used to obtain the address of the register variable (the address in C + + can be taken).

(3) Only local automatic variables and formal parameters can be used as register variables, others (such as global variables) do not.

A function is called to occupy some registers to hold the value of the register variable, and the register is freed after the function call. Thereafter, these registers can be used to hold the register variables of the function when another function is called.

(4) A local static variable cannot be defined as a register variable. cannot be written as: register static int A;

(5) Because of the limited number of registers (different number of CPU registers), you cannot define any number of register variables, and some registers can only accept certain types of data (such as pointers and floating point numbers), so the number and type of register modifiers that really work depends on the machine that runs the program. Any extra register modifiers will be ignored by the compiler. In particular, modern compilers are more intelligent, and in general we do not need to actively use register variables.

C:

C++:

Why can C + + take an address to a register variable?

The C + + compiler ignores your register request when it encounters an address to the register variable.

2. Variable detection enhancement

Global variables:

int A;

int a=1;

C language can be compiled through (and without any warning), in C + + is not allowed. In C, an error occurs only if the global variable of the same name is given an initial value, for example:

int a=1;

int a=1;

Therefore, the detection of variables in C + + is more stringent.

3.const of reinforcement

As already mentioned in the previous essay, the const of C is not a constant, but a constant in C + +.

4. Enhancement of the three-mesh operator

The three-mesh operator expression in the C language cannot be an lvalue, but it is possible in C + +.

5.struct keyword Enhancement

This is naturally needless to say, the class is one of the features of C + +, this is not much to say. Do a test that illustrates the difference between C + + struct and C-language struct definition "structure".

6. Enhancement of function return value and parameter detection

C language has some historical problems, the function can not have the return value, parameters can also not declare the type, but in C + + is not allowed, C language we should also avoid such a writing.

C-language function parameters do not write, representing everything, C + + function parameters are not written, representing void.

C C's strengthening

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.