The similarities and differences between const basic knowledge, const in C + + and C, and the definition of const and # define macros

Source: Internet
Author: User

1.const Basic Knowledge

(1) Defining constants with const

const int a = 5; Defines an int type constant A with a value of 5

(Note: It is stored in the symbol table (Key,value) in C + + and does not allocate memory space, only allocates memory when the address is taken or is defined as a global variable for use in other files)

(2) const and pointer

1 Const int // P1 is a pointer to constant a, the value of a cannot be changed by the pointer P1 2 3 int    Const P2 =&b; // P2 is a pointer to B only, and the value of B can be changed by P2 4 Const int Const P3 =&c;   // P3 is a constant pointer to C only, and the value of C cannot be changed by the pointer P3!         

(3) const and reference

Weekend repair ...

The similarities and differences of 2.const in C + + and C

In C is "hypocrite", "counterfeit" ...

 1  void   main ()  2  3  const  int  a = 5  ;  4  int *p = &A;  5  *p =  6  printf ( " a =%d   " ,a);  7 } 

In the above code we use the const definition of the constant A, in the C language can be pointed to a pointer p to modify the content of a, this is inconceivable, so we say that the Const in C is "hypocrite", "counterfeit", and did not play his role. But

In C + + is a veritable.

The reason is also mentioned above, that is, the constants defined in C + + are stored in the symbol table (key,value), do not allocate memory space, only when the address or defined as a global variable is used in other files to allocate memory space, we use constant constant name to access the constant when using only the constants in the symbol table, Changes to the constant values in the memory space do not affect the values stored in the symbol table.

The similarities and differences of 3.constyu#define macro definition

Weekend Supplement ...

The similarities and differences between const basic knowledge, const in C + + and C, and the definition of const and # define macros

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.