Difference between const in C and C ++

Source: Internet
Author: User

Under normal circumstances, const in C ++ is regarded as a constant during the compilation period. The Compiler does not allocate space for const, but stores the period value in the name table during compilation, and in the code when appropriate. therefore, the following code:

#include using namespace std;int main(){    const int a = 1;    const int b = 2;    int array[ a + b ] = {0};    for (int i = 0; i < sizeof array / sizeof *array; i++)    {        cout << array << endl;    }    return 0;}

After compilation and normal operation, but slight modification, put it in the C compiler, an error will occur:

#include int main(){    int i;    const int a = 1;    const int b = 2;    int array[ a + b ] = {0};    for (i = 0; i < sizeof array / sizeof *array; i++)    {printf("%d",array);    }    return 0;}

Error message:
C: \ test1 \ te. C (8): Error c2057: enter a constant expression.
C: \ test1 \ te. C (8): Error c2466: An array with a constant of 0 cannot be allocated.
The reason for this is:
In C, const is a common variable that cannot be changed. Since it is a variable, it occupies storage space. Therefore, the compiler does not know the value at compile time. in addition, the subscript of the array definition must be a constant.
In C:
Const int size;
This statement is correct because it is regarded by the C compiler as a declaration indicating that the bucket is allocated elsewhere. however, this write in C ++ is incorrect. const in C ++ is an internal connection by default. To achieve the above effect in C ++, you must use the extern keyword.
In C ++, const uses internal connections by default. In C, external connections are used.
Internal join: the compiler only creates a bucket for the files being compiled. Other files can use the same identifier.
Or use the static keyword to specify the connection in the global variable. C/C ++.
External Connection: create a separate bucket for all compiled files. Once the bucket is created, the connector must be unbound.
References to this bucket. The global variables and functions are connected externally. The key is extern.
Statement to access the corresponding variables and functions from other files.

/*********************** C ++ code *********** * ****************** // header. h const int test = 1;
 //test1.cpp #include  #include "header.h" using namespace std; int main() {     cout << "in test1 :" << test << endl;     return 0;  }  //test2.cpp #include  #include "header.h" using namespace std; void print() {     cout << "in test2:" << test << endl; } 

The above code compilation connection won't go wrong, but if you change header. h:
Extern const int test = 1;
The following error message is displayed during connection:
Test2 error lnk2005: "int const test "(? Test @ 3hb) has been defined in test1.obj
Because the extern keyword tells the C ++ compiler that test will be referenced elsewhere, the C ++ compiler will create a bucket for test, instead of simply storing it in the name table. therefore, when two files contain both headers. h, there will be a name conflict.
This situation is similar to const in C:

 //header.h const int test = 1;  //test1.c #include  #include "header.h" int main() {     printf("in test1:%d\n",test);     return 0; }  //test2.c #include  #include "header.h" void print() {     printf("in test2:%d\n",test); } 

Error message:
Test3 fatal error lnk1169: locate one or more symbols with multiple definitions
Test3 error lnk2005: _ test has been defined in test1.obj
In C ++, whether to allocate space for the const depends on the specific situation.
If the keyword extern is added or the const variable address is obtained, the compiler allocates storage space for the const.
When defining constants in c ++, define is no longer used, because define only performs simple macro replacement and does not provide type check.

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.