Home | Projects | Notes > C Programming > const Type Qualifier

const Type Qualifier

 

const Type Qualifier

const is a type qualifier in C used to enforce read-only feature on variables. It adds some safety to your code. The compiler will warn you when you try to modify the value of the const variable.

Use const generously to enforce pointer access restrictions while writing functions and function prototypes.

Using const may also help the compiler generate the optimized code.

 

How to Use const Type Qualifier?

Syntax:

Some programmers prefer the second way because it helps interpret a complex statement better (i.e., Reading backwards). First, identify the variable name, then read from left to right.

 

constness of a Variable

By using the const keyword, you (as a programmer) are just making a promise to the compiler that you won't try to modify the content of the variable using its name.

If you try to modify the variable by its name, the compiler will stop you by throwing an error (i.e., compile-time error).

However, you can still modify the content of the variable by using a pointer of an appropriate type that points to that variable.

const does NOT mean that the value will never change. It is just a programming safety feature to prevent the programmer from modifying the value in a certain way.

 

Placement of const Variables in Memory

All local const variables are just like non-const variables as far as memory placement is concerned. They are placed in RAM. The only specialty of a const variable is that it is read-only.

All global const variables are stored in ROM or FLASH. This also further depends on the linker script rules and the hardware on which the code runs.

For example, in STM32 target hardware, all global const variables reside in the FLASH memory. So, when you try to modify the const variable using its address, the operation will have no effect because the FLASH memory of the microcontroller is write-protected.

 

Usage of const Type Qualifier

Case 1: Constant data

Syntax:

Use case:

To define mathematical constants in the program

Case 2: Modifiable pointer to constant data

Syntax:

Here, the pointer pData is modifiable, but from pData's perspective the memory location it points to is not modifiable (read-only). Note that this does not necessarily mean that the memory location itself is non-modifiable in nature. It's just that pData recognizes it that way because of the way pData is declared.

Use case:

In the following function, src is guarded to prevent the data pointed to by src from being modified by mistake.

This usage can be found in many system call declarations:

Case 3: Constant pointer to modifiable data

Syntax:

Here, the pointer pData is not modifiable (read-only), but from pData's perspective the memory location it points to is modifiable.

Use case:

Use to improve readability and guard the pointer variables.

Case 4: Constant pointer to constant data

Syntax:

Here, the pointer pData is not modifiable (read-only), and from pData's perspective the memory location it points to is also not modifiable (read-only).

Use case:

Use when reading data from the status register of the hardware. Contents of the status register shouldn't be modified by the programmer.

 

More Examples

The first const keyword can be on either side of the type:

More examples on const usage: