Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there const in C?

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++?
like image 370
Armen Tsirunyan Avatar asked Mar 09 '11 16:03

Armen Tsirunyan


People also ask

What is the const in C language?

The const keyword allows a programmer to tell the compiler that a particular variable should not be modified after the initial assignment in its declaration.

Is const a data type in C?

In C, C++, and D, all data types, including those defined by the user, can be declared const , and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified.

Should I use const or define in C?

In general, const is a better option if we have a choice and it can successfully apply to the code. There are situations when #define cannot be replaced by const. For example, #define can take parameters (See this for example). #define can also be used to replace some text in a program with another text.


2 Answers

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 */ 
like image 178
AnT Avatar answered Sep 21 '22 03:09

AnT


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.

like image 35
Ben Voigt Avatar answered Sep 23 '22 03:09

Ben Voigt