Type qualifiers for variables

The c programming language defines type qualifiers which control how variables may be accessed and modified. The two type qualifiers are const and volatile

const

const is used to tell the computer that the value of this variable cannot be changed. When you create a const variable it has to be given a value at the time of creation. Once given a value its value cannot be changed.

const int i ;  i = 10;  // This code is wrong. You cannot create a const variable  // without initializing it with a value at the time of creation.    // Look at the code below    const int i = 10; // This is correct.  i = 20; // This is wrong. The value of the const              // cannot be changed.

volatile

By marking a variable volatile you are telling the compiler that

  1. Its value can be changed either by the program itself or by some external factors ( like hardware interrupts ) and therefore it should always be read from memory and not from any cache storage.
  2. Apply no optimization to the variable. Like for example when you read variable inside a loop the compiler will feel that you are reading the same variable again and again and it will try to maintain a copy of it to make the process fast and not read the variable from memory every time. But when you mark that variable as volatile you are telling the compiler that no matter how inefficient it is, please read the variable from memory every time and do not cache its value.

So by making variable volatile you are introducing some inefficiency to your code but making sure that the read and write operations will always do what you intend them to do.

Using const and volatile together

const says that the value of the variable cannot be changed and volatile says that the value of the variable can be changed by some factors outside the program so it value should always be read and never be cached. They both seem to be two opposite concepts. So can we use const and volatile together ?

The answer is yes.

Take a case when you are working with some hardware and reading data from a port. You need to tell the compiler that

  1. The port address from where we are reading should always be read only. Don't allow write operation to that port.
  2. The information at that port address can change so please always read it and don't locally store it to make the program more efficient.

Therefore you can make it both const and volatile like this

int const volatile *port = ( const volatile int *) 0x10;

In this line of code please note that

  1. int is the type specifier.
  2. const and volatile are type qualifiers
  3. The order in which you use them does not matter

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp