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

2

11

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.

Dessau answered 7/2, 2020 at 9:51 Comment(9)
So many answers just a Google search away. One of them : #4486942Brancusi
@Brancusi No, the answers there actually point no difference between the languages. Only how they behave in a certain language.Dessau
In C, const doesn't have anything to do with linkage. You can have static const at file scope and it has internal linkage,Spirelet
Apart from that, the obvious difference would be that C++ can make member functions const qualified, to block the function from changing values of class members. C doesn't have that since it doesn't have classes.Spirelet
If you're unhappy with the current answers to that question -- which is the same as yours -- consider posting a bounty on it.Nigrify
I agree that the linked duplicate is bad. A good answer would list all of the differences and not so much explaining what const does the same in both languages.Spirelet
I can attempt to write such an answer but I'm not enough of a C++ guru to be sure I've got all the differences covered. On the top of my head: const variables in C++ are constant expressions, unlike in C. C++ can const qualify member functions. The mentioned linkage. Anything else?Spirelet
@Spirelet - For your first comment. - Yes, I know. I just wanted to point out that f.e.const int a = 34; without static has external linkage by default, while static const int a = 34; indeed has internal linkage.Dessau
@Spirelet That would be awesome. If you do not want to cover all even a good base is great. Also you could classify the answer, if you want, a tag-wiki answer. BTW, I´ve seen you have a C++ score of 1,900. Don´t degrade yourself.Dessau
S
14
  • 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.

Spirelet answered 7/2, 2020 at 10:45 Comment(6)
Can you have member functions in C?Tati
Note that const size_t n = 1; static int array[n]; only works if the compiler can see the definition of n and do constant propagation. extern const size_t n; static int array[n]; doesn't work.Tati
Hm, I rather see such hard ware registers being addressed via pointers, such like uint32_t const* x = reinterpret_cast<uint32_t const*>(20102012);...Polymerize
@Polymerize That would make such registers incompatible with the rest of the register map. And how would that enable you to view the actual register values in a debugger? For example if you just want to view the read-only silicon mask set registers to quickly see what part you've ended up with. And obviously, you'd also need to volatile qualify the pointer.Spirelet
@Spirelet Admitted, didn't pay attention on the volatile... Rest depends. The debuggers I had by hand in these cases could easily resolve *x as well. On the other hand, if the registers are mapped to some memory region and the compiler doesn't support placing variables at specific memory locations directly (I've seen both), getting the variable at a specific memory location sometimes can get a bit messy (having to cover that in the map file...). In the end I don't much care about having a variable located at the right place or a pointer, as long as I can get the task I'm assigned to done ;)Polymerize
@Spirelet I´ve added more parts from the original source to the cited quote to show the context that the qoute is part of a point/type for internal linkage.Dessau
T
0

From cppreference.com:

The const qualifier used on a declaration of a non-local non-volatile non-template (since C++14) non-inline (since C++17) variable that is not declared extern gives it internal linkage. This is different from C where const file scope variables have external linkage.

Other than that const has the same semantics in C and C++ and C headers with const are often compiled as C++ headers with conditional "extern C".

Tati answered 7/2, 2020 at 10:17 Comment(8)
It's a bad quote, oversimplification. static const x; at file scope in C has internal linkage. Linkage of a C variable is determined by which scope it is declared in, as well as the presence/absence of storage class specifiers. const and other type qualifiers doesn't play part of it at all.Spirelet
@Spirelet How is that different from what the quote says?Tati
The example I just gave proves the quote wrong. According to the quote, static const x; at file scope in C has external linkage.Spirelet
@Spirelet The quote says that int const x = 1 in C has external linkage. Hence, you'd need static to change the linkage to internal. The quote is pretty clear to me, unlike your comments.Tati
It really does not say that at all. Read the quote. "...C where const file scope variables have external linkage".Spirelet
@MaximEgorushkin Lundin means that the sentence "This is different from C where const file scope variables have external linkage." shall rather be "This is different from C where const file scope (without having static qualifier before const) variables have external linkage." to be 100% correct and not-ambiguous.Dessau
@RobertSsupportsMonicaCellio The original quote is quite unambiguous to me. Just an FYI: cppreference is a wiki, you can edit it.Tati
Well, that's a matter of interpretation. Some consider const and static const variables being two distinct sets without intersection, others consider the latter being a subset of the former. Depending on this or that interpretation, the sentence is correct or wrong. Ambiguity? Well, as long as we haven't agreed on which definition applies, it is...Polymerize

© 2022 - 2024 — McMap. All rights reserved.