Declaration differences between C and C++ const

  • 2020-05-09 18:57:46
  • OfStack

When you declare a variable as const in the C source code file, you can do this by:


const int i = 2;

You can then use this variable in another module, as follows:


extern const int i;

However, to get the same behavior as in C++, the const variable must be declared as:


extern const int i = 2;

If you want to declare the extern variable for the C source file in the C++ source file, use:


extern "C" const int x = 10;

To prevent name renormalization by the C++ compiler.


Related articles: