Const in C Language

Source: Internet
Author: User

Const in C Language

Const in C language indicates "unchangeable variable", or can be a "pseudo constant"

Const in C ++ is called a constant with type descriptions.

Const int liv_num = 10; liv_num = 18; // error because liv_num is modified by const, it cannot be directly assigned a value.

So why do I say "pseudo constants"?

 

We know that there are two ways to assign values to variables in c: direct assignment and indirect assignment.

For the first method, we have verified that it is not possible. So, can the liv_num value be changed by indirect assignment?

Note: it must be a. c file. If it is. cpp, it will not work, because const in c ++ is called "a constant with type description".

 

# Include

 
  
# Include

  
   
Int main () {// define a constant const int liv_num = 10; // liv_num = 18; // error because liv_num is modified by const, it cannot be directly assigned a value. // define a pointer to the constant const int * pliv_num = & liv_num; // convert the pointer pointing to a constant to int * pNum = (int *) pliv_num; // assign the value * pNum = 20; printf (liv_num = % d, liv_num ); // result: 20 system (pause); return 0 ;}

  

 

Through the above, we will direct the const to "pseudo constant" in c, which also shows a problem. The const is saved in the "stack". If you want to verify it, you can use debug, observe the memory.

 

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.