Define and const

Source: Internet
Author: User
(1) Different define macros are processed by compilers in the preprocessing phase. Const constants are used in the compilation and running phase. (2) Different Types and security checks define macros have no types, and do not perform any type checks, just expand. Const constants have specific types. during compilation, the type check is performed. (3) Different Storage Methods define macros only expand. The memory will be expanded as many times as there are places to use and will not be allocated. Const constants are allocated in memory (either in heap or stack ). (4) const can save space and avoid unnecessary memory allocation. For example: # define PI 3.14159 // constant macro const doulbe Pi = 3.14159; // pi is not put into ROM at this time ...... double I = PI; // at this time, the PI is allocated with memory and will not be allocated later! Double I = PI; // macro replacement during compilation, allocating Memory double J = PI; // no memory allocation double J = PI; // then macro replacement, allocate memory again! Const defines constants. From the assembly point of view, only the corresponding memory address is given, rather than the number of immediate values as # define. Therefore, the const-defined constants have only one copy during the program running, while the # define-defined constants have several copies in the memory.

Define and const

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.