Is there const in C?

21,671

Solution 1

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

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

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

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

Two other differences:

Share:
21,671
Armen Tsirunyan
Author by

Armen Tsirunyan

Updated on July 13, 2022

Comments

  • Armen Tsirunyan
    Armen Tsirunyan almost 2 years

    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++?
  • Armen Tsirunyan
    Armen Tsirunyan about 13 years
    And these differences are... ? :)
  • Ben Voigt
    Ben Voigt about 13 years
    @Armen: Patience, padawan, patience.
  • Kris
    Kris about 13 years
    C89, actually (or C90, whatever it's actually called).
  • Homunculus Reticulli
    Homunculus Reticulli over 11 years
    I see we meet again Mr Bond ... or shall I say Mr Voight?. You used to answer my questions on comp.lang.c++ waaay, back in the day :)
  • David Rodríguez - dribeas
    David Rodríguez - dribeas over 10 years
    +1 Did not know this specifics of the difference between C and C++
  • Dániel Sándor
    Dániel Sándor over 7 years
    "In C++, const variables of appropriate type are integral constant expressions" - they can be, but they aren't in all cases. I guess that is the main reason for adding consexpr keyword to ISOC++11. Example: int i=0; std::cin>>i; const int I=i; int is[I]; The last command is illegal according to the standard, because I is not a static expression, however it is a constant variable. So a more correct statement would be: "In C++, const variables of appropriate type CAN BE integral constant expressions, but in C they can be never".
  • Peter Mortensen
    Peter Mortensen about 4 years
  • django11
    django11 about 4 years
    @PeterMortensen: yeah perhaps I should have said "since first ANSI C (aka C89)" but as noted in that Wikipedia article "Historically, the names [ANSI C] referred specifically to the original and best-supported version of the standard (known as C89 or C90)." and "This version [C89] of the language is often referred to as ANSI C"