Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the const qualifier in C and the const qualifier in C++?

I´ve found a comment of user R..:

C and C++ are not the same language. In particular, C const has nothing to do with C++ const.

I know, that one difference between the const qualifier in C and the const qualifier in C++ is its default linkage.

An object declared at namespace scope with const qualifier in C++ has internal linkage, while in C an object with const qualifier declared at global scope (without having a static qualifier before const) has external linkage.

But how else do they both differ between the languages of C and C++? I´ve thought both have the same kind of concept and purpose in both languages.

My Question:

  • What is the difference between the const qualifier in C and the const qualifier in C++?

The answers to How does "const" differ in C and C++? do not point an exact difference between the languages of C and C++ in the context of the const qualifier. Only what you can´t do or can do with it in a certain language.

like image 539
RobertS supports Monica Cellio Avatar asked Feb 07 '20 09:02

RobertS supports Monica Cellio


People also ask

What is const qualifier in C?

In general, the const qualifier is used to declare a variable as constant, meaning its value cannot change once the variable has been initialized.

What are the differences between the const and volatile qualifiers in embedded C?

The const keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter. The volatile keyword specifies that the value associated with the name that follows can be modified by actions other than those in the user application.

What is const type qualifier?

The const qualifier explicitly declares a data object as something that cannot be changed. Its value is set at initialization. You cannot use const data objects in expressions requiring a modifiable lvalue. For example, a const data object cannot appear on the lefthand side of an assignment statement.

What is the difference between const qualifier and define directive?

#define is a preprocessor directive. Data defined by the #define macro definition are preprocessed, so that your entire code can use it. This can free up space and increase compilation times. const variables are considered variables, and not macro definitions.


1 Answers

  • The most important difference is that in C++ a const variable is a constant expression (even prior the introduction of C++11 constexpr), but a const variable in C is not.

    Meaning that C++ allows you to do things like const size_t n = 1; static int array[n]; but C does not allow that, supposedly for historical reasons.

  • In C++, const plays part in determining linkage. This is different between C++ versions. According to cppreference.com (emphasis mine):

    Any of the following names declared at namespace scope have internal linkage:


    • non-volatile non-template (since C++14) non-inline (since C++17) non-exported (since C++20) const-qualified variables (including constexpr) that aren't declared extern and aren't previously declared to have external linkage;

    Whereas in C, const does not play part in determining linkage at all - only declaration scope and storage class specifiers matter.

  • In C++, you can const qualify member functions. This isn't possible in C since it doesn't have syntax support for member functions.

  • C allows const-qualified variables to be declared without an initializer. In C, we can write const int x; without initializers, but C++ does not allow that. At a glance, this may seem like a senseless language bug in C, but the rationale is that computers have read-only hardware registers with values set by hardware, not software. Meaning that C remains suitable for hardware-related programming.

like image 111
Lundin Avatar answered Oct 06 '22 18:10

Lundin