Type qualifiers modify how an object can be accessed or used. The three standard C type qualifiers are const, volatile, and restrict.
#include <stdio.h>
int main()
{
const int maximum = 100;
volatile int status = 0;
printf("%d\n", maximum);
printf("%d\n", status);
return 0;
}
Example Output #
100
0
Explanation #
const indicates that an object cannot be modified through the qualified lvalue:
const int maximum = 100;
An assignment such as:
maximum = 200;
is not allowed.
volatile indicates that accesses to an object may have observable effects outside the normal flow assumed by the compiler:
volatile int status = 0;
It is commonly used for objects that can change because of hardware or other execution contexts.
restrict is a qualifier used with pointers. It provides an optimization-related aliasing guarantee when the pointer is used according to the requirements of the qualifier.
int * restrict ptr;
Type Qualifiers #
| Qualifier | Purpose |
|---|---|
const |
Prevents modification through the qualified access |
volatile |
Indicates that accesses to the object must be treated as potentially externally observable |
restrict |
Provides a restricted pointer-aliasing contract |
_Atomic |
Specifies atomic types or atomic-qualified access in C11 and later |
The qualifiers can also be combined:
const volatile int status;
Here, status is both const and volatile.