C - Const Qualifier



The const qualifier in C is used to declare variables whose values should remain fixed throughout the program. It is used to declare a variable as a constant, whose value cannot be changed after initialization. Once a variable is declared as const, it becomes read-only, and the compiler prevents any accidental modifications.

What is the Use of const Qualifier in C?

Using the const qualifier, programmers can improve the safety, readability, and maintainability of their program codes. It helps programmers make their intentions clear by signaling which values should stay fixed throughout the program.

For example, constants like mathematical values (PI, e, etc.), array sizes, or configuration values should not be altered at runtime. By declaring them as const, we protect these values from accidental changes.

Example of Using const Qualifier in C

Take a look at the following example. Here, we have used const to ensure the value of PI remains fixed throughout the program.

#include <stdio.h>

int main() {
   // Defining a constant variable
   const int PI = 3.14;

   printf("Value of PI: %d\n", PI);

   // Trying to modify PI will cause an error
   // PI = 10; // Not allowed, PI is read-only

   return 0;
}

Observe how the const variable (PI) prevents unwanted changes to constants. When we run this code, it will produce the following output -

Value of PI: 3

Using const Qualifer in Different Contexts

Apart from defining constant variables, the const qualifier can also be used in different contexts to provide various behaviors. Here are some different scenarios where we can use the const qualifier:

  • Pointer to Constant
  • Constant pointer to variable
  • Constant pointer to constant

In the subsequent sections of this chapter, we will cover each of these topics in detail.

Pointer to Constant

As its name suggests, a "pointer to a constant" points to a constant value which cannot be modified. However, the pointer itself can change because it's a variable and it can point somewhere else.

Let's see its syntax −

const type* name;

Or,

typeconst* name;

A "pointer to a constant" means the pointer can point to different variables, but the value of the object being pointed to cannot be changed through this pointer.

  • The pointer itself is stored in the read-write area (such as a stack in this case).
  • The object being pointed to may reside either in the read-only area or in the read-write area.
  • Even if the object is stored in the writable location, you cannot alter its value using this pointer, since the pointer treats the object as a constant.

Example: Pointer to Constant with an Integer

In this example, we are using a pointer to constant with an integer −

#include <stdio.h>

int main() {
   int x = 10, y = 20;

   // Pointer to constant integer
   const int *ptr = &x;
   printf("Value pointed by ptr: %d\n", *ptr);

   // *ptr = 15;  // Not allowed (cannot change value through ptr)
   ptr = &y;  	// allowed (pointer can point to another variable)
   printf("Value pointed by ptr: %d\n", *ptr);

   return 0;
}

Notice that we cannot change the value of the variable "x" though the pointer. On executing the code, you will get the following output

Value pointed by ptr: 10
Value pointed by ptr: 20

Example: Pointer to Constant with an Array

In this example, we are using a pointer to constant with an array −

#include <stdio.h>
int main() {
   int arr[] = {1, 2, 3};

   // Pointer to constant integer (array base address)
   const int *ptr = arr;
   printf("First element: %d\n", *ptr);

   // *ptr = 5;  	// not allowed (cannot change array element via ptr)
   ptr++;  	// Allowed (can move pointer to next element)
   printf("Second element: %d\n", *ptr);

   return 0;
}

When we run the above program, it will produce the following output

First element: 1
Second element: 2

Here, we cannot modify an element of the array using a pointer in a regular way. Notice the following commented line in the program −

// *ptr = 5;

Constant Pointer to Variable

A constant pointer means the pointer itself is a constant variable; it means we cannot alter the pointer to point to another variable. Its syntax is as follows −

int* const ptr;

Let's understand this concept using the following example. It demonstrates that the value of the object pointed by the constant pointer can be changed but the pointer cannot point to another variable.

#include <stdio.h>
int main(void) {
   int i = 10;
   int j = 20;
   
   /* constant pointer to integer */
   int* const ptr = &i;
   printf("ptr: %d\n", *ptr);

   *ptr = 100; // change the pointer value
   printf("ptr: %d\n", *ptr);
   // ptr = &j; // pointer cannot point to another variable
   return 0;
}

Here is the output of the above code −

ptr: 10
ptr: 100

Notice the commented line −

// ptr = &j; 	// pointer cannot point to another variable

If we uncomment this line and run the code, then it will produce an error. Why? It's because the constant pointer (ptr) is pointing to "i" and it cannot point to another variable "j".

Constant Pointer to Constant

A "constant pointer to constant" is a type of constant pointer that points to a constant variable, which means we cannot alter the value pointed to by the pointer, also the pointer cannot point to other variables. Let’s see its syntax

const int* const ptr;

The following example shows how you can use a "constant pointer to constant" in a C program −

#include <stdio.h>

int main(void){
   int i = 10;
   int j = 20;

   /* constant pointer to constant integer */
   const int* const ptr = &i;
   printf("ptr: %d\n", *ptr);

   ptr = &j;	// error
   *ptr = 50;	// error
   return 0;
}

Now, let's run this code and check its output

ERROR!
main.c: In function 'main':
main.c:10:9: error: assignment of read-only variable 'ptr'
   10 |     ptr = &j; // error
      |         ^
ERROR!
main.c:11:10: error: assignment of read-only location '*(const int *)ptr'
   11 |     *ptr = 50; // error

In the code, observe that we are trying to modify the pointer as well as the value it is point to. And, a "constant pointer to constant" denies both the actions, which is why we are getting an error in both the cases.

Advantages of Const Qualifier

Following are the advantages of using a const qualifier in a C program −

  • Helps prevent unintentional changes to variables
  • Reduces the risk of bugs and errors in your code; makes the code more reliable
  • Prevents functions from modifying arguments that should remain unchanged

By using const qualifiers, we can avoid having to make a copy of their values, which can reduce the memory usage and improve performance.

Conclusion

The const qualifier in C is a powerful feature that improves code readability, safety, and reliability. It helps prevent accidental modification by clearly defining which values should not change. It supports compiler optimizations and makes programs error-free and easy to maintain.

Advertisements