Const Qualifier in C | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed ( Which depends upon where const variables are stored, we may change the value of const variable by using pointer ).
The result is implementation-defined if an attempt is made to change a const:

  1. Pointer to variable
    int *ptr;
    We can change the value of ptr and we can also change the value of object ptr pointing to. Pointer and value pointed by pointer both are stored in the read-write area. See the following code fragment.
    #include <stdio.h>
    int main(void)
    {
        int i = 10;
        int j = 20;
        int *ptr = &i;
        /* pointer to integer */
        printf("*ptr: %d\n", *ptr);
        /* pointer is pointing to another variable */
        ptr = &j;
        printf("*ptr: %d\n", *ptr);
        /* we can change value stored by pointer */
        *ptr = 100;
        printf("*ptr: %d\n", *ptr);
        return 0;
    }
    Output:
    *ptr: 10
        *ptr: 20
        *ptr: 100
  2. Pointer to constant
    Pointer to constant can be declared in following two ways.
    const int *ptr;
    or
    int const *ptr;
    We can change the pointer to point to any other integer variable, but cannot change the value of the object (entity) pointed using pointer ptr. The pointer is stored in the read-write area (stack in the present case). The object pointed may be in the read-only or read-write area. Let us see the following examples.
    #include <stdio.h>
    int main(void)
    {
        int i = 1θ;  
        int j = 2θ;
        /* ptr is pointer to constant */
        const int *ptr = &i;
        printf("ptr: %d\n", *ptr);
        /* error: object pointed cannot be modified
        using the pointer ptr */
        *ptr = 1θθ;
        ptr = &j;          /* valid */
        printf("ptr: %d\n", *ptr);
        return θ;
    }
    Output:
    error: assignment of read-only location ‘*ptr’
    Following is another example where variable i itself is constant.
    #include <stdio.h>
    int main(void)
    {  
        /* i is stored in read only area*/
        int const i = 1θ;    
        int j = 2θ;
        /* pointer to integer constant. Here i
        is of type "const int", and &i is of
        type "const int *".  And p is of type
        "const int", types are matching no issue */
        int const *ptr = &i;
        printf("ptr: %d\n", *ptr);
        /* error */
        *ptr = 1θθ;
        /* valid. We call it up qualification. In
        C/C++, the type of "int *" is allowed to up
        qualify to the type "const int *". The type of
        &j is "int *" and is implicitly up qualified by
        the compiler to "const int *" */
        ptr = &j;
        printf("ptr: %d\n", *ptr);
        return θ;
    }
    Output:
    error: assignment of read-only location ‘*ptr’
    Down qualification is not allowed in C++ and may cause warnings in C. Following is another example with down qualification.
    #include <stdio.h>
    int main(void)
    {
        int i = 1θ;
        int const j = 2θ;
        /* ptr is pointing an integer object */
        int *ptr = &i;
        printf("*ptr: %d\n", *ptr);
        /* The below assignment is invalid in C++, results in error
           In C, the compiler *may* throw a warning, but casting is
           implicitly allowed */
        ptr = &j;
        /* In C++, it is called 'down qualification'. The type of expression
           &j is "const int *" and the type of ptr is "int *". The
           assignment "ptr = &j" causes to implicitly remove const-ness
           from the expression &j. C++ being more type restrictive, will not
           allow implicit down qualification. However, C++ allows implicit
           up qualification. The reason being, const qualified identifiers
           are bound to be placed in read-only memory (but not always). If
           C++ allows above kind of assignment (ptr = &j), we can use 'ptr'
           to modify value of j which is in read-only memory. The
           consequences are implementation dependent, the program may fail
           at runtime. So strict type checking helps clean code. */
        printf("*ptr: %d\n", *ptr);
        return θ;
    }
  3. Constant pointer to variable
    int *const ptr;
    Above declaration is a constant pointer to an integer variable, means we can change the value of object pointed by pointer, but cannot change the pointer to point another variable.
    #include <stdio.h>
    int main(void)
    {
       int i = 1θ;
       int j = 2θ;
    /* constant pointer to integer */
       int *const ptr = &i;
       printf("ptr: %d\n", *ptr);
       *ptr = 1θθ;    /* valid */
       printf("ptr: %d\n", *ptr);
       ptr = &j;        /* error */
       return θ;
    }
    Output:
     error: assignment of read-only variable ‘ptr’
  4. Constant pointer to constant
    const int *const ptr;
    Above declaration is a constant pointer to a constant variable which means we cannot change value pointed by the pointer as well as we cannot point the pointer to other variables. Let us see with an example.
    #include <stdio.h>
    int main(void)
    {
        int i = 1θ;
        int j = 2θ;
    /* constant pointer to constant integer */
        const int *const ptr = &i;
        printf("ptr: %d\n", *ptr);
        ptr = &j;     /* error */
        *ptr = 1θθ;   /* error */
        return θ;
    }
    Output:
    error: assignment of read-only variable ‘ptr’
    error: assignment of read-only location ‘*ptr’
The document Const Qualifier in C | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Const Qualifier in C - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is the purpose of the const qualifier in C?
Ans. The const qualifier in C is used to declare a variable as read-only, meaning its value cannot be modified after initialization. It helps in enforcing immutability and enhances program correctness and maintainability.
2. Can const qualifier be applied to function parameters in C?
Ans. Yes, the const qualifier can be applied to function parameters in C. When a function parameter is declared as const, it means that the function promises not to modify the value of that parameter. This helps in preventing accidental modifications and allows the compiler to perform optimizations.
3. What is the difference between const and #define in C?
Ans. The const keyword in C is used to declare a variable as read-only, with a specific type and scope. On the other hand, #define is a preprocessor directive that performs textual substitution. It is used to define constants or to create macros. Unlike const, #define does not have a specific type, and it is a simple text replacement operation performed by the preprocessor.
4. Can a const variable be modified in C?
Ans. No, a const variable cannot be modified after initialization in C. Any attempt to modify its value will result in a compilation error. The purpose of the const qualifier is to enforce immutability and prevent accidental modifications.
5. What are the benefits of using const qualifier in C?
Ans. Using the const qualifier in C provides several benefits, such as: - Enhanced program correctness by preventing accidental modifications to variables. - Improved code maintainability by clearly expressing the intent of immutability. - Potential compiler optimizations, as the const qualifier allows the compiler to make assumptions about variable values. - Better code documentation by indicating variables that should not be modified, making the code more readable and understandable.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

practice quizzes

,

Free

,

pdf

,

ppt

,

Exam

,

Extra Questions

,

mock tests for examination

,

Objective type Questions

,

Semester Notes

,

Const Qualifier in C | Programming and Data Structures - Computer Science Engineering (CSE)

,

shortcuts and tricks

,

past year papers

,

Sample Paper

,

video lectures

,

MCQs

,

Summary

,

Const Qualifier in C | Programming and Data Structures - Computer Science Engineering (CSE)

,

Viva Questions

,

Previous Year Questions with Solutions

,

Const Qualifier in C | Programming and Data Structures - Computer Science Engineering (CSE)

,

study material

,

Important questions

;