#define宏, const keyword C + +

Source: Internet
Author: User

First, #define宏定义

1. Macro definition, like this is the macro definition: #define PI 3.14159;

2. Macro Definition Format: Macro value #define Macro name is the macro to be defined, usually denoted by a meaningful name in uppercase, and the macro value is the constant represented by the macro. ;

3. #define是预编译指令;

4. The essence of the macro is an alternative.

5. When the precompiled program processes the source code, if it finds that the macro is used in the code, the macro value is used instead of the macro.

6. Benefits of using Macros: Use a macro to give the constant a birthright, in addition to avoiding multiple inputs repeated use;

7. Example:

Macros make your code more concise and straightforward

#define MIN 0

#define MAX 100

for (int i =min; i<max; ++i)

{//....

}

Situations where macros are not used

for (int i =0;i <100;++i)

{

//..

}

8. The problem with macros: because it is an unconditional substitution at preprocessing, and does not explicitly specify the data type of the constant, it is easy to bring convenience as well as problems. So the Const keyword is out of the field;

Second, the const keyword

1. The effect is this: const double pi= 3.14159;//defines a constant PI;

2. The format is: const data type constant name = constant value

3. Another use of the keyword, so that the value of the variable can not be modified, as long as the variable is defined by adding the const keyword;

4. Once a constant with const is defined, it can no longer be modified, or a compilation error will occur.

General above: The use of the Const keyword is recommended

#define宏, const keyword C + +

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.