The difference between a const constant and a # define macro constant

Source: Internet
Author: User

http://blog.csdn.net/lifengguo_njupt/article/details/7992332

C + + can define constants with const, or you can define constants using # define, but the former has more advantages than the latter:

1,const constants have data types, and macro constants do not have data types, and the compiler can perform static type security checks on the former, but only character substitution, no type safety checks, and unexpected errors in character substitution (marginal effect)

2, some integrated debugging tools can debug const constants, but cannot debug macro constants.

Therefore, you should use const to define symbolic constants, including string constants, in C + + as much as possible.

Things that are modified by const are protected by the static type security mechanism implemented by C++/C language, which can prevent accidental modification and improve the robustness of the program.

The difference between a const constant and a # define macro constant

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.