
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
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.