C Type Qualifiers

Type qualifiers are are nothing but a prefix added before declaring a variable to provide it with some additional functionality.

There are four types of type qualifiers in C.

  1. const
  2. volatile
  3. restrict (C99)
  4. _Atomic (C11)

Const

The const type qualifier is used to make a variable constant i.e. the value of the variable cannot be changed once initialized (can be changed using pointers).

Syntax

const int var;
//or
int const var;

Volatile

Volatile qualifier is used to tell the compiler that the value of the variable may change unexpectedly and no optimization should be done. A variables can change unexpectedly in the following ways.

  1. Memory-mapped peripheral registers
  2. Global variables modified by an interrupt service routine (ISR)
  3. Global variables being accessed by multi-threaded applications.

Syntax

volatile int var;
//or
int volatile var;

Restrict

The restrict type qualifier is introduced since C99 onwards.The restrict qualifier allows only a pointer to accesses the underlying object. It eliminates the potential for pointer aliasing, enabling better optimization by the compiler.

Syntax

int * restrict ptr;

Atomic

The atomic type qualifier is introduced since C11 onwards. It is used to designates a new atomic type. It can be mixed with const, restrict, or volatile but unlike other qualifiers the atomic version of data type may have a different size, alignment, and object representation.

Syntax

_Atomic const int * ptr;

Trending

C Programming

Remember to follow community guidelines and don't go off topic.