volatile modifier in c

All variable in c are by default not volatile. With help of modifier volatile which is keyword of c language you can make any variable as volatile variable.
Properties of volatile variable:

1. A volatile variable can be changed by the background routine of preprocessor. This background routine may be interrupt signals by microprocessor, threads, real times clocks etc.

2. In simple word we can say a value volatile variable which has stored in the memory can be by any external sources.

3. Whenever compiler encounter any reference of volatile variable is always load the value of variable from memory so that if any external source has modified the value in the memory complier will get its updated value.

4. Working principle of volatile variable is opposite to the register variable in c. Hence volatile variables take more execution time than non-volatile variables.

A volatile variable is declared with help of keyword volatile:

int volatile i;

A non-volatile variable is declared without using keyword volatile:

int i;

Question: What is meaning of following declaration in c?
const volatile float f;
register volatile char c;

3 comments:

volatile said...

Volatile keyword says the compiler that no optimiztion on the variable.The volatile keyword acts as a data type qualifier.

My Thoughts in SW Engineering said...

The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code.

Unknown said...

const volatile char c; it means the value will be modified only in case of hardware. they cannot modified in software case.