The keywords which are used to modify the properties of a variable are called type qualifiers / modifiers. Type qualifiers are a fairly advanced feature which not all programs need const : A const value is one you promise not to modify volatile : A volatile value is one that might change unexpectedly
Type qualifiers / modifiers express the way in which a variable is accessed or where a variable is stored in the memory by keeping the meaning or interpretation of the variable same. In a way, type qualifiers add more refinement to variables.
Use the const type qualifier to qualify an object whose value cannot be changed. Objects qualified by the const keyword cannot be modified. This means that an object declared as const cannot serve as the operand in an operation that changes its value; for example, the ++ and -- operators are not allowed on objects qualified with const . Using the const qualifier on an object protects it from the side effects caused by operations that alter storage.
Declaration of const
const int x = 44; /* const qualification of int type --
the value of x cannot be modified */
const int *z; /* Pointer to a constant integer --
The value in the location pointed
to by z cannot be modified */
int * const ptr; /* A constant pointer -- a pointer
which will always point to the
same location */
const int *const p; /* A constant pointer to a constant
integer -- neither the pointer or
the integer can be modified */
const const int y; /* Illegal - redundant use of const */
Rules of using const type qualifier
const struct employee {
char *name;
int birthdate; /* name, birthdate, job_code, and salary are */
int job_code; /* treated as though declared with const. */
float salary;
} a, b; /* All members of a and b are const-qualified*/
struct employee2 {
char *name;
const int birthdate; /* Only this member is qualified */
int job_code;
float salary;
} c, d;
All members in the first structure are qualified with const . If the tag employee is used to specify another structure later in the program, the const qualifier does not apply to the new structure's members unless explicitly specified.
const int i = 0;
int j = 1;
const int *p = &i; /* Explicit const specifier required */
int *q = &j;
*p = 1; /* Error -- attempt to modify a const-
qualified object through a pointer */
*q = 1; /* OK */
The type qualifier volatile means that the value of the variable marked volatile may be changed in other ways that are not specified by the program. The variables that are volatile change usually due to some external factors and not necessarily because of the program. In other words, they are volatile in nature.
Declaration of volatile
volatile char keyboard_ch; /* a volatile variable */
Rules of using volatile type qualifier
For Example
volatile volatile int x;
For Example
volatile struct employee {
char *name;
int birthdate; /* name, birthdate, job_code, and salary are */
int job_code; /* treated as though declared with volatile. */
float salary;
} a,b; /* All members of a and b are volatile-qualified */
struct employee2 {
char *name;
volatile int birthdate; /* Only this member is qualified */
int job_code;
float salary;
} c, d;
If the tag employee is used to specify another structure later in the program, the volatile qualifier does not apply to the new structure's members unless explicitly specified.
For Example
const int *intptr;
volatile int x;
intptr = &x;
Likewise, the address of a volatile object can be assigned to a pointer that points to a non-volatile object