The const and define of C language

Source: Internet
Author: User

Const modifies a read-only variable, not a constant, whose value cannot be used at compile time because the compiler does not know what it is storing at compile time. The compiler usually does not allocate storage space for ordinary const read-only variables, but keeps them in the symbol table, which makes him a compile-time value, without the storage and memory operations, making it highly efficient.

For example:

#define M 3//Macro constant

const int n=5; N is not put in memory at this time

······

int n=i; n allocates memory at this time and is no longer assigned

int i=m; Macro substitution during precompilation, allocating memory

int j=n; No memory allocated

int j=m; To replace the macro again and allocate memory again

It is known that a const-defined read-only variable has only one copy of the program running (because it is a global read-only variable, stored in the static area), and the macro constants defined by # define have several copies in memory.

#define宏是在预编译期间进行替换, no type

A const-Modified read-only variable has a specific type, which determines its value at compile time.

Const modifier pointer:

const int *p; P variable, p-pointing object is not variable

int const *P; P variable, p-pointing object is not variable

int *const p; P is immutable, the object to which P points is variable

const int *const p; The P and P objects are not variable

How to interpret it?

Ignore the type name (compiler parsing will also be ignored type name), see the const from which to go, who is near to decorate who.

 

The const and define of C language

Related Article

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.