• admin@embedclogic.com

Type Qualifiers in C

Qualifiers are nothing but an addition of a prefix keyword with existing datatypes to add some extra feature in a declared variable.

There are two qualifiers in C –

  1. const
  2. volatile

const in c

const keyword is used to make some variable constant in the program.

Example:

1.int x=5;

x is an integer variable which holds some data 5 and it can be changed to some other value anywhere in the program.

2. const int x=5;

x is a constant integer variable which holds some data 5 and it can not be changed to some other value anywhere in the program.

Points to remember about const

1. ‘const‘ keyword can use with all data types whether is fundamental or derived.

2. const keyword freezes the data after initialization and you can not modify in the whole program.

We can see that empId is constant so it can only read but not write.

3. const variable without initialization can freeze itself with some garbage data and it can’t be changed later at runtime.

volatile Qualifier in C

volatile keyword mostly used with the variable which can expect change from outside word also i.e by external peripheral or some other thread.

volatile keyword tells the compiler not to optimise code related the variable usually, when we know it can be changed from “outside”.

 volatile unsigned int vui_var;

Point to remember for volatile

A compiler can not optimize the volatile variable i.e every time variable access from its real memory location not from cache or somewhere else.

The volatile variable may also have const qualifier if someone doesn’t want to modify in a program.But this variable can change from outside peripheral or thread e.g status register