The const and Cplusplus Const__c language of C language

Source: Internet
Author: User
Tags modifier

(Do not know why the title write C + + always does not appear, should be csdn new Markdown editor Bug) C + + const and C language #define can be used to define constants, the two are different, const is a constant of data types, and macro constants do not, The compiler can perform static type security checks on the former, which is only character substitution and no type security check.
The C language of the Const and C + + is also very different, in C language with the const modified variable is still a variable, indicating that the variable is read-only, can not be displayed changes, and in C + + after the use of the const modifier, it becomes a constant. For example, the following code:

const int n=10;
int a[n];

These two lines of code will have an error in the C language because the length of the array when declaring the array must be constant, and the n inside this is a variable in the C language. After using the const modifier in C + +, n is already equal to a constant, so it can be passed.
Then look at the following code:

const int a=3;
int* p=&a;
*p=4;

This is not allowed in C + + because A is a constant after a const modifier, so it is not allowed to be modified, either by explicitly changing the value of a or by modifying its value by other means.

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.