Difference between define macro definition and const constant definition

Source: Internet
Author: User
Tags constant definition define null

Define section:
Macros can be used not only to replace constant values, but also to replace expressions or evenCode. (Macros have powerful functions but are prone to errors, so their advantages and disadvantages are quite controversial .)

Macro Syntax:

# Define macro name macro value

Note that the macro definition is not a strict statement in the C or C ++ sense, so the end of the row does not end with a plus sign.

As a suggestion and a broad rangeProgramIn common, macro names often use uppercase letters.

Advantages of macros:

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

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. 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 Section

Constant Definition Format:

Const data type constant name = constant value;

The constants defined by const have data types. constants defining data types are convenient for the compiler to perform data checks, so that programs may encounter Errors for troubleshooting. A constant must be specified at the beginning. Then, in future code, we cannot change the value of this constant.

Differences between the two:
Memory space allocation. When define is used for macro definition, no memory space is allocated. during compilation, It will be replaced in the main function, but it will be replaced simply without any check, such as the type and statement structure, that is, macro-defined constants are purely placement relationships, for example, # define null 0; when the compiler encounters null, it always uses 0 instead of null. It has no data type. (For more information, see C language books for preprocessing or msdn. the constants defined by const have data types. constants defining data types are convenient for the compiler to perform data checks, so that programs may encounter Errors for troubleshooting, therefore, the difference between const and define is that const defines constants to eliminate the security of programs.

Http://www.phpweblog.net/maple094/archive/2008/05/30/4054.html

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.