Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how const keyword works in c

Tags:

c++

c

I want to know about const internals in c and c++ . How compiler imposes constantness ? Can some one help me please.

like image 496
mousey Avatar asked May 11 '10 17:05

mousey


People also ask

What is const in C example?

A constant is a value or variable that can't be changed in the program, for example: 10, 20, 'a', 3.4, "c programming" etc.

Does C have the const keyword?

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++.

What is the use of const keyword explain with an example?

We use the const qualifier to declare a variable as constant. That means that we cannot change the value once the variable has been initialized. Using const has a very big benefit. For example, if you have a constant value like the value of PI, you wouldn't like any part of the program to modify that value.

What is the function of const?

The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.


2 Answers

In general const is 100% compiler. When you declare something const, the compiler places restrictions on what it will let you write. It won't let you assign to const scalar, assign through a const reference or or pointer, or invoke a non-const function of const object.

There is no guarantee that the compiler will arrange any sort of runtime protection.

like image 162
bmargulies Avatar answered Oct 08 '22 01:10

bmargulies


const keyword in C and C++ has two different semantic meanings.

(1) It can declare the constness of an object

const SomeType t;

In the above case object t is a non-modifiable object. The compiler will do its best to prevent you from modifying it by observing the const-correctness rules (which are not the same in C and C++). The const-correctness rules are enforced only conceptually, at the language level, which means that there are ways to circumvent these rules, and which also means that constness of an object will not be necessarily implemented at physical level. I.e. there's no guarantee that the object will be ultimately placed in read-only memory.

It is important to note that this kind of constness is not removable in a sense that any attempts to modify the above object by casting away the constness lead to undefined behavior (excluding possible mutable members in C++).

(2) It can declare constness of an access path to an object

const SomeType *p;

The above p is declared as a pointer-to-const. This does not necessarily mean that the object p is pointing to is a constant object (as defined by the first kind of const above). It might easily be a non-constant one, in which case it is perfectly legal to cast away the constness from the above access path and modify the object, although it is generally not a good programming practice. In other words, constness of an access path is potentially removable.

Taking the above into account, the following declaration

const int* const* const* const p = 0;

includes two different kinds of const: the very last const declares the constness of the object p (first kind), while the rest of const declare the constness of various levels of access path represented by p (second kind).

P.S. As a [possibly unrelated] side note, it is probably worth noting that the term constant has drastically different meanings in C and C++. In C++ constants are objects declared as const. In C constants are literals. Objects declared as const are not constants in C terminology.

like image 32
AnT Avatar answered Oct 08 '22 01:10

AnT