12

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.

9
  • 1
    @schaiba No, the answers there actually point no difference between the languages. Only how they behave in a certain language. Commented Feb 7, 2020 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, Commented Feb 7, 2020 at 10:06
  • 3
    If you're unhappy with the current answers to that question -- which is the same as yours -- consider posting a bounty on it. Commented Feb 7, 2020 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. Commented Feb 7, 2020 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? Commented Feb 7, 2020 at 10:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Can you have member functions in C?
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.
Hm, I rather see such hard ware registers being addressed via pointers, such like uint32_t const* x = reinterpret_cast<uint32_t const*>(20102012);...
@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 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 ;)
|
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".

8 Comments

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 How is that different from what the quote says?
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 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.
It really does not say that at all. Read the quote. "...C where const file scope variables have external linkage".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.