Const Qualifier In C

Example

const Data_Type Identifier = Value;

In essence, the const keyword simply informs the compiler that the value of a const-qualified object remains constant. Essentially, const signifies immutability, indicating that no new value can be assigned to the object during runtime.

However, if we define a symbol as constant by using the const keyword, the location where the "constant variable" is placed in the process control block is determined by the implementation (or the compiler). As a result, different systems may store the constants and jump tables in the text segment, which exclusively houses and processes all other executable commands.

Rules

There exist several guidelines to assist us in determining the appropriate times to utilize a const in a C program:

We may opt for it when there is no intention to modify the value of the variable post initialization.

Similarly, when dealing with call by reference, it proves beneficial when there is a need to maintain the original value of the passed variable. For instance,

Example

int Display ( const char *pcMessage)
  • Another application involves its utilization for linking I/O registers using pointers in the C programming language.

Now, let's explore some instances where we'll employ the const keyword in our code alongside a pointer:

1. Pointer to constant

Syntax

Example

const int *xData;
or
int const *xData;

As illustrated in the declaration above, the variable "xData" is assigned to a constant integer. Consequently, modifications to the integer variable referenced by the pointer (*xData) are not permitted, but the pointer can be redirected to point to a different integer variable. This flexibility arises from the fact that pointers are typically stored in the read and write memory section. An example demonstrating this concept will be presented in the following sections.

Example 1

In this instance, we aim to modify the integer variable's value by leveraging the constant pointer (piIndex). Nevertheless, attempting to alter the variable's value (iIndexData1) using *piData will result in a compiler error.

Program

Example

#include <stdio.h>
int main(void)
{
/*Integer variable*/
int iIndexData1 = 2;
/*pointer to const int*/
const int *piIndex = NULL;
/*Assign the address of iIndexData to a pointer to const,here integer variable qualify to const integer its called up qualification which is valid in c*/
piIndex = &iIndexData1;
*piIndex = 3; /*Try to change value constant object*/
printf("Data is %d",*piIndex);
return 0;
}

Output

Error occurred in function 'main' due to attempting to assign a value to a read-only location '*piIndex'.

Example

*piIndex = 3;

Example 2

Now, we are going to attempt to modify the reference variable in this instance.

Program

Example

#include <stdio.h>
int main(void)
{
/*Integer variable1*/
int iIndexData1 = 2;
/*Integer variable2*/
int iIndexData2 = 4;
/*Assign address of iIndexData to pointer to const,
here integer variable qualify to const integer*/
const int* piIndex = &iIndexData1;
printf("*piIndex is %d\n",*piIndex);
/*It's invalid because pointer is constant*/
piIndex = &iIndexData2;
printf("*piIndex is %d\n",*piIndex);
return 0;
}

Output

Output

*piIndex is 2
*piIndex is 4

You can observe that the code provided above functions flawlessly, and we have effectively modified the reference variable.

2. ConstanLogic Practiceer to a constant:

Syntax

Example

const int *const piData;

As evident in the declaration above, it specifies that the constanLogic Practiceer is referencing an integer constant variable. This implies that the value pointed to by the pointer cannot be altered, and the pointer itself cannot be reassigned to other integer variables.

Example 1

In this instance, we will apply the idea of a constant operator to a fixed value.

Program

Example

#include <stdio.h>
int main(void)
{
//Integer variable1
int Data = 2;
//consLogic Practiceer to const int
const int *const piData = &Data;
printf("*piData is %d\n",*piData);
return 0;
}

Output

Output

*piData is 2

As evident, the functionality remains intact when there are no attempts to modify the values stored in piData and *piData.

Example 2

In this instance, we aim to modify the value of piData. Nonetheless, as per the principle, we will encounter a compiler error since piData is designated as a constant. Let's observe the outcome:

Program

Example

#include <stdio.h>
int main(void)
{
//Integer variable
int Data = 2;
//consLogic Practiceer to const int
const int *const piData = &Data;
//changing the value
*piData = 3;
printf("*piData is %d\n",*piData);
return 0;

Output

Output

Error: assignment of read-only location '*piData'

As demonstrated in the Output, it is imperative that the value of "*pidata" remains unchanged.

Example 2

Here we will explore the concept of assigning another integer variable to the pointer and observe the outcome:

Program

Example

#include <stdio.h>
int main(void)
{
/*Integer variable1*/
int Data1 = 2;
/*Integer variable2*/
int Data2 = 4;
const int* const piData = &Data1;
//Display data
printf("*piData is %d\n",*piData);
/*It's invalid because pointer is constant*/
piData = &Data2;
printf("*piData is %d\n",*piData);
return 0;

Output

Output

Error: assignment of read-only variable 'piData'

In the result of both aforementioned programs, it is evident that when a constant is declared, its value cannot be modified, and it cannot be reassigned to point to a different variable.

Input Required

This code uses input(). Please provide values below: