About define and const

Source: Internet
Author: User

1. The constants defined by define are generally called macro definitions in the C language. The essence of define is simple text substitution.

2.const defines a variable, but the value of the variable can only be assigned at the time of definition, and then it cannot be changed.

    • If the variable declaration has the keyword const, you cannot modify the value of the variable by assignment, increment, or decrement.
    • A const anywhere on the left of the * makes the data a constant, and a const on the right makes the pointer itself a constant.

3.(1) different compiler processing methods

Define macros are expanded during the preprocessing phase.

Const constants are used during the compile run phase.

(2) Different types and security checks

Define macros have no type, do not do any type checking, just expand.

Const constants have specific types that perform type checking during the compilation phase.

(3) different storage methods

The Define macro is only expanded, how many places are used, how many times it is expanded, and no memory is allocated.

Const constants are allocated in memory (either in the heap or in the stack).

(4) const can save space and avoid unnecessary memory allocation. For example:
#define PI 3.14159//Macro Constants
Const DOULBE pi=3.14159; Pi is not placed in ROM at this time ...
Double i=pi;//allocates memory for Pi at this time and is no longer assigned!
Double i=pi;//macro substitution during compilation, allocating memory
Double j=pi;//No memory allocation
Double j=pi;//to replace the macro again, allocate memory again!
Const definition constants from the assembly point of view, just give the corresponding memory address, instead of the immediate number as given in # define, so the const definition of the constant in the program run only one copy, and #define定义的常量在内存中有若干个拷贝.
(5) Improved efficiency. The compiler typically does not allocate storage space for ordinary const constants, but instead saves them in the symbol table, which makes it a constant during compilation, without the storage and read memory operations, making it highly efficient.

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