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.

  • So many answers just a Google search away. One of them : https://stackoverflow.com/questions/4486442/how-does-const-differ-in-c-and-c – schaiba Feb 07 '20 at 09:54
  • 1
    @schaiba No, the answers there actually point no difference between the languages. Only how they behave in a certain language. – RobertS supports Monica Cellio Feb 07 '20 at 10:04
  • 1
    In C, `const` doesn't have anything to do with linkage. You can have `static const` at file scope and it has internal linkage, – Lundin Feb 07 '20 at 10:06
  • 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. – Lundin Feb 07 '20 at 10:07
  • 3
    If you're unhappy with the current answers to that question -- which is the same as yours -- consider posting a bounty on it. – Sneftel Feb 07 '20 at 10:09
  • 4
    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. – Lundin Feb 07 '20 at 10:09
  • 1
    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? – Lundin Feb 07 '20 at 10:14
  • @Lundin - 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. – RobertS supports Monica Cellio Feb 07 '20 at 10:27
  • @Lundin 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. – RobertS supports Monica Cellio Feb 07 '20 at 10:32

2 Answers2

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Can you have member functions in C? – Maxim Egorushkin Feb 07 '20 at 10:47
  • 1
    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. – Maxim Egorushkin Feb 07 '20 at 11:00
  • Hm, I rather see such hard ware registers being addressed via pointers, such like `uint32_t const* x = reinterpret_cast(20102012);`... – Aconcagua Feb 07 '20 at 11:55
  • @Aconcagua 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. – Lundin Feb 07 '20 at 12:41
  • @Lundin 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 ;) – Aconcagua Feb 07 '20 at 14:06
  • @Lundin 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. – RobertS supports Monica Cellio Feb 07 '20 at 18:55
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".

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • 1
    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. – Lundin Feb 07 '20 at 10:21
  • @Lundin How is that different from what the quote says? – Maxim Egorushkin Feb 07 '20 at 10:26
  • 1
    The example I just gave proves the quote wrong. According to the quote, `static const x;` at file scope in C has external linkage. – Lundin Feb 07 '20 at 10:28
  • @Lundin 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. – Maxim Egorushkin Feb 07 '20 at 10:29
  • 1
    It really does not say that at all. Read the quote. "...C where const file scope variables have external linkage". – Lundin Feb 07 '20 at 10:31
  • @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. – RobertS supports Monica Cellio Feb 07 '20 at 10:37
  • @RobertSsupportsMonicaCellio The original quote is quite unambiguous to me. Just an FYI: cppreference is a wiki, you can edit it. – Maxim Egorushkin Feb 07 '20 at 10:41
  • 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... – Aconcagua Feb 07 '20 at 10:57