Back to C Programming
2026-07-125 min read

C - Const Qualifier

Learn C - Const Qualifier step by step with clear examples and exercises.

Why This Matters

In this full guide, we will delve into the const qualifier in C programming, an essential concept that helps you write cleaner, more efficient code. By understanding how to use const effectively, you will not only improve your coding skills but also prepare yourself for real-world programming scenarios, interviews, and debugging challenges.

Why This Matters

The const qualifier plays a crucial role in C programming by allowing you to declare constants, which are variables that cannot be modified during runtime. Constants help reduce unnecessary memory allocation, prevent unintended changes, and improve the overall efficiency of your code.

Prerequisites

To fully grasp the core concept of const qualifiers, it is essential that you have a solid understanding of the following topics:

  1. Basic syntax and data types in C
  2. Variables and their lifetimes
  3. Pointers and pointer arithmetic
  4. Understanding the difference between compile-time and runtime
  5. Familiarity with functions, function arguments, and return types

Core Concept

The const qualifier is used to declare a variable as a constant, meaning its value cannot be changed during runtime. There are two types of constants in C:

  1. Compile-time constants: These are denoted by the #define preprocessor directive and have no storage location. They must be defined before they are used.
  2. Variables declared with const: These occupy memory and can be initialized at runtime, but their values cannot be modified after initialization.

Const-qualified pointers

A pointer can also be declared as const, which means the pointer itself cannot be modified but the data it points to can still be changed. Conversely, if a pointer points to a const object, both the pointer and the object remain constant.

const int myNum = 5;
int *const ptr = &myNum;

// Correct: Change the value of myNum through the pointer
*ptr = 10; // myNum is now 10

// Incorrect: Trying to change the address stored in ptr
ptr = &anotherNum; // Compile-time error

In this example, myNum is a constant integer, and ptr is a const pointer pointing to myNum. Changing the value of myNum through the pointer is allowed, but trying to change the address stored in ptr results in a compile-time error.

Const-qualified arrays

You can also declare arrays with the const qualifier, which means the array elements cannot be modified during runtime.

const int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // ptr points to a const integer
*ptr = 10; // Compile-time error: assignment of read-only location

In this example, arr is an array of constant integers. Declaring a pointer ptr pointing to the first element of the array and trying to modify its value results in a compile-time error.

Worked Example

Let's consider a simple example where we use const to ensure that function arguments are not modified unintentionally:

#include <stdio.h>

void printNum(const int num) {
printf("The number is: %d\n", num);
}

int main() {
int myNum = 5;

// Calling the function with a const argument ensures myNum remains unchanged
printNum(myNum);

// Changing myNum after calling the function has no effect on its value inside the function
myNum = 10;
printf("myNum is now: %d\n", myNum);
printNum(myNum);

return 0;
}

In this example, we define a function called printNum that takes a const integer as an argument. By doing so, we ensure that the function cannot modify its arguments during runtime, maintaining the integrity of our code and preventing unintended side effects.

Common Mistakes

  1. Forgetting to initialize const variables: It is essential to initialize const variables at declaration, as they cannot be modified afterward.
const int myNum; // Compile-time error: initializer expression required
  1. Declaring pointers as const but not initializing them: Pointers declared as const must be initialized before use to avoid compile-time errors.
const int *ptr; // Compile-time error: assignment of read-only location
  1. Trying to modify a constant variable through a non-const pointer: If you declare a non-const pointer to a const variable, you cannot modify the value of the const variable through that pointer.
const int myNum = 5;
int *ptr = &myNum; // ptr points to a const integer
*ptr = 10; // Compile-time error: assignment of read-only location
  1. Modifying the address stored in a const pointer: Since const pointers cannot be modified, you cannot change the address they point to.
const int *ptr = &myNum;
ptr = &anotherNum; // Compile-time error: assignment of read-only location

Practice Questions

  1. Explain the difference between compile-time constants and variables declared with const.
  2. Given the following code, what is the output?
const int myNum = 5;
int *const ptr = &myNum;
printf("%d\n", myNum);
*ptr = 10;
printf("%d\n", myNum);
  1. Write a function called swap that takes two const integers as arguments and swaps their values without using temporary variables.
  2. Explain the difference between a constant pointer and a pointer to a constant. Provide examples for both cases.
  3. Can you declare a 2D array with the const qualifier in C? If so, provide an example.
  4. What happens when you try to modify a constant array element through a non-const pointer?
  5. How can you create a function that returns a const integer in C?

FAQ

Can I declare an array with the const qualifier?

Yes, you can declare an array with const by declaring each element as const or the entire array as const.

const int arr[5] = {1, 2, 3, 4, 5}; // Each element is a constant
int const arr2[5] = {1, 2, 3, 4, 5}; // Entire array is a constant

Can I declare a function with the const qualifier?

No, functions cannot be declared as const in C. However, you can make function arguments and return types const to ensure that they are not modified during runtime.

What happens if I try to modify a constant variable after initialization?

Trying to modify a constant variable after initialization results in a compile-time error. The value of the constant variable cannot be changed once it has been initialized.

Can I declare a 2D array with the const qualifier in C?

Yes, you can declare a 2D array with the const qualifier by declaring each element as const or the entire array as const.

const int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int *const ptr = &arr[0][0]; // ptr points to a const integer
*ptr = 10; // Compile-time error: assignment of read-only location

What happens when you try to modify a constant array element through a non-const pointer?

Trying to modify a constant array element through a non-const pointer results in a compile-time error. The value of the constant array elements cannot be changed once they have been initialized.

How can you create a function that returns a const integer in C?

You can create a function that returns a const integer by declaring its return type as const int and ensuring that it does not modify any of its local variables or arguments during execution.

const int getSum(const int a, const int b) {
return a + b;
}