C-Language const and Cplusplus const

Source: Internet
Author: User

(Do not know why the title write C + + always show, should be csdn new Markdown editor Bug) C + + const and C # # can be used to define constants, the two are different, const is a constant with data type, and the macro constant is not, The compiler can perform static type security checks on the former, which is only character substitution and no type safety 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, non-display changes, and in C + + after the const modification, it becomes a constant. For example, the following code:

constint n=10;int a[n];

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

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

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

C-Language const and Cplusplus const

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.