The difference between C-language const and define

Source: Internet
Author: User

The const definition is that the variable is not a constant, but the value of this variable is not allowed to change the constant variable! With type. A type check exists when the compilation runs.

define defines a constant without a type, with only a simple character substitution. It works at pre-compilation time and does not exist for type checking.

1, the difference between the two

(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

    • #define宏仅仅是展开, how many places are used, how many times it is expanded, and no memory is allocated. (macro definition does not allocate memory, variable definition allocates memory.) )
    • 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:

#defineNum3.14159 Macro ConstantsConstDoulbeNum = 3.14159; Pi is not placed in ROM at this time ...double I =num;//allocates memory for PI at this time and is no longer assigned! double I= NUM  //during compilation macro substitution, allocating memory double  J = num; //no memory allocation double J =< Span class= "PLN" > Num;//again for macro substitution, once again allocating memory!   

Const definition constants from the assembly point of view, just give the corresponding memory address, and not as #define given is the immediate number, so the const definition of the constant in the program run only one copy (because it is a global read-only variable, there is a static zone), and #define The defined constants have several copies in memory.

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

(6) The macro substitution is replaced only, does not do the calculation, does not do the expression solves;

When the macro is precompiled, it is replaced, and the program runs without allocating memory.

The difference between C-language const and define

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.