[Reference] const and define

Source: Internet
Author: User
Tags constant definition

Constant:

Representing constants with macros

If we want to write a program for various computation of circles, then the round (3.14159) value will be widely used. We obviously have no reason to change the value of limit, so we should treat it as a constant. So, do we have to write the 3.14159 long string over and over again? The macro is used. Macros can be used not only to replace constant values, but also to replace expressions or even code segments. Macro Syntax:

# Define macro name macro value

For example, to replace the limit value mentioned above, it should be: # define PAI 3.14159. Note that the macro definition is not a statement in the strict sense of C or C ++, so the end of the row does not end with a plus sign. The macro name naming rule is the same as the variable name. Therefore, PAI is used to represent the macro, because C and C ++ cannot directly use the sequence character. With the above statement, we can use the PAI macro to replace all the places where 3.14159 is used in the program. Macro names often use uppercase letters. Suppose there was a piece of code: double zc = 2*3.14159 * R; // calculates the circumference, where R represents the radius Variable double mj = 3.14159 * R; // calculates the circular area.

After macro PAI is defined, we can use it like this:

# Define PAI 3.14159

Double = 2 * PAI * R; // calculates the circumference, where R is the variable representing the radius

Double = PAI * R; // calculates the circular area.

Replacing constants with macros has the following advantages:

1) make the code more concise and clear

Of course, this depends on a proper name for the macro. Generally, the macro name should be more explicit and intuitive, and sometimes it is better to keep it longer.

2) Easy code Maintenance

As mentioned above, 3.14159. When you find that the accuracy value is not accurate enough and you want to change it to 3.1415926, you can modify only one macro instead of all macros in the code.

Original macro:

# Define PAI 3.14159

Modified macros:

# Define PAI 3.1415926

Macro processing is called "preprocessing" during compilation ". That is to say, before official compilation, the compiler must replace the Macros in the Code with their corresponding macro values. In this process, you may find and replace them in the text processing software. After preprocessing is completed, all the original "PAI" become 3.1415926 immediately. Therefore, when macro expressions are used in the Code, the immediate number is used in the final analysis, and the type of this number is not explicitly specified. This may cause some problems, so C ++ uses another more secure method to replace the macro function.

Const data type constant name = constant value;

Compared with the format defined by the variable, the constant definition must start with const. In addition, the constant must be assigned a value while being defined.

Const float PAI = 3.1415926; (Note the semicolon) 

The function of const is to specify that the volume (PAI) is a constant rather than a variable.

A constant must be specified at the beginning. In future code, we cannot change the value of PAI, for example:

Const float PAI = 3.14159;

Double zc = 2 * PAI * R;

PAI = 3.1415926; // Error !, PAI cannot be modified.

Double mj = PAI * R;

If a constant is an integer, You can omit the data type, for example:

Const k = 100;

Equivalent

Const int k = 100;

Conversely, if the data type is not specified, the compiler treats it as an integer. For example:

Const k = 1.234;

Although you want k to be equal to a real number, the final k value is actually 1. Because the compiler regards it as an integer constant.

We recommend that you specify the type when defining a variable, whether it is an integer or another type.

Const int I = 100;

Const double di = 100.0;

Const and # define

# Define defines the name of a constant, that is, you can use this name as a constant and replace it with this constant by the compiler during compilation. This is only to improve readability, but security cannot be guaranteed.
For security considerations, c ++ introduces the const definition. Of course, this is only one of the functions of const. With const, you can define a variable that cannot modify its value, that is, it can be used as a constant. Of course, this amount is different from 100, \ 'a \ ', and so on. The difference is that this volume has its own memory address, which is allocated space.
In general, # define does not allocate space to the name, but just gives a constant a name. Const defines a variable whose value cannot be modified and has its own address in the memory.

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.