Is there const in C?

C++CConstantsLanguage Comparisons

C++ Problem Overview


This question may be naive, but:

  • is there const keyword in C?
  • since which version?
  • are there any semantic and/or syntactic differences between const in C and C++?

C++ Solutions


Solution 1 - C++

There are no syntactic differences between C and C++ with regard to const keyword, besides a rather obscure one: in C (since C99) you can declare function parameters as

void foo(int a[const]);

which is equivalent to

void foo(int *const a);

declaration. C++ does not support such syntax.

Semantic differences exist as well. As @Ben Voigt already noted, in C const declarations do not produce constant expressions, i.e. in C you can't use a const int object in a case label, as a bit-field width or as array size in a non-VLA array declaration (all this is possible in C++). Also, const objects have external linkage by default in C (internal linkage in C++).

There's at least one more semantic difference, which Ben did not mention. Const-correctness rules of C++ language support the following standard conversion

int **pp = 0;
const int *const *cpp = pp; // OK in C++

int ***ppp = 0;
int *const *const *cppp = ppp; // OK in C++

These initializations are illegal in C.

int **pp = 0;
const int *const *cpp = pp; /* ERROR in C */

int ***ppp = 0;
int *const *const *cppp = ppp; /* ERROR in C */

Generally, when dealing with multi-level pointers, C++ says that you can add const-qualification at any depth of indirection, as long as you also add const-qualification all the way to the top level.

In C you can only add const-qualification to the type pointed by the top-level pointer, but no deeper.

int **pp = 0;
int *const *cpp = pp; /* OK in C */

int ***ppp = 0;
int **const *cppp = ppp; /* OK in C */

Another manifestation of the same underlying general principle is the way const-correctness rules work with arrays in C and C++. In C++ you can do

int a[10];
const int (*p)[10] = &a; // OK in C++

Trying to do the same in C will result in an error

int a[10];
const int (*p)[10] = &a; /* ERROR in C */

Solution 2 - C++

The first two questions are answered here: Const in C

Yes there are quite a few differences in semantics between const in C and C++.

  • In C++, const variables of appropriate type are integral constant expressions (if their initializers are compile-time constant expressions) and can be used in context which requires that, such as array bounds, and in enum definitions. In C, they are not and cannot be.

  • In C++, const global variables automatically have static linkage, so you can put them in header files. In C, such variables have external linkage and that would generate duplicate definition errors at link time.

Solution 3 - C++

Yes, there is a const keyword. It was added as part of the 1989 standard.

As far as compatibility, here's a paragraph from Harbison & Steele, 5th edition:

A top-level declaration that has the type qualifier const but no explicit storage class is considered to be static in C++ but extern in C. To remain compatible, examine top-level const declarations and provide an explicit storage class.

In C++, string constants are implicitly const; they are not in C.

Solution 4 - C++

Yes, const has been there since at least since ANSI C (aka C89).

It certainly appears in my copy of "The C Programming Language (2nd Edition)", Kernighan & Ritchie (published in 1988).

Relevant extract:

> The const and volatile properties are new with the ANSI standard. The purpose of const is to announce objects that may be placed in read-only memory, and perhaps to increase opportunities for optimization.

Solution 5 - C++

Two other differences:

Solution 6 - C++

Yes. const is there in C, from C89.

Here is a good read is about behaviour of const keyword in C.

Solution 7 - C++

The semantic in C is different than in C++. For example,

unsigned const a = 10;
unsigned A[a];

in file scope would be valid in C++, but not in C.

Solution 8 - C++

Yes, there's a const keyword in C. It's been there since C90.

Syntactically, it can occur in the same places as in C++. Semantically, it's a bit more lax, IIRC.

Solution 9 - C++

According to ESR, const was added in the ANSI C Draft Proposed Standard. Eric Giguere's summary of ANSI C, dated 1987, confirms it.

This looks like the draft itself -- search for "3.5.3 Type qualifiers".

Solution 10 - C++

There is a "const" keyword in C, and it has been for a long time. If a variable is designated "const", writes to it are forbidden.

Additionally, in some environments, variables declared "const" may be located in a different data segment from other variables. This data segment may offer hardware write protection, and for embedded systems, may be stored in ROM or flash memory rather than in RAM (a very important distinction on some processors which have a lot more ROM or flash than RAM--e.g. 128 KB flash and 3.5 KB RAM, or 2 KB ROM and 96 bytes RAM).

Note that the compiler will generally not make any inferences about "const" values or expressions involving them. If I say "const char foo[] = "Hello";" and then later make reference to foo[1], the compiler will load the value (which will most likely be 'e') from wherever foo[] is stored and use the loaded value. Sometimes this usefully allows values to be patched in a compiled code image, but sometimes it just wastes code.

If you want to define a number to to be a compile-time "substitutable" constant, the best way, at least for integer constants, may be to use "enum". For example, "enum {woozle=19;}" will cause 19 to be substituted for "woozle" throughout the code. Note that unlike textual substitutions; enum declarations obey proper rules of scope.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArmen TsirunyanView Question on Stackoverflow
Solution 1 - C++AnTView Answer on Stackoverflow
Solution 2 - C++Ben VoigtView Answer on Stackoverflow
Solution 3 - C++John BodeView Answer on Stackoverflow
Solution 4 - C++GrahamSView Answer on Stackoverflow
Solution 5 - C++Johannes Schaub - litbView Answer on Stackoverflow
Solution 6 - C++NawazView Answer on Stackoverflow
Solution 7 - C++Jens GustedtView Answer on Stackoverflow
Solution 8 - C++Anthony WilliamsView Answer on Stackoverflow
Solution 9 - C++KenView Answer on Stackoverflow
Solution 10 - C++supercatView Answer on Stackoverflow