Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Const Qualifier in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

The programming language C is procedural. Dennis Ritchie created it for the first time in 1972. It was primarily developed as a programming language for creating operating systems. Low-level memory access, a minimal set of keywords, and a clean style are among the properties that make the C language suited for system programs, such as operating systems or compiler development. In this blog, we will learn about Const Qualifier in C.

Also See, Sum of Digits in C, C Static Function

Read more, odd or even program in c

Const Qualifier in C

The Const Qualifier is used to specify a variable as a constant, which means that its value will not change after it is initialised. However, there are some advantages to using const, such as the fact that if the PI has a constant value, we don't want any part of the program to change it. As a result, we may use the const qualifier to declare it as a const. However, whether we can change the value of the variable declared using Const Qualifier or not depends on where the const variables are stored in the memory.

Pointer to constant

Declaration

const int *ptr;  
// or
int const *ptr;  

Here 'ptr' is pointing to a const int. So here, we can change the Pointer to point to some other variable but cannot change the value of the pointed variable.

Example

#include <stdio.h>

// Pointer to constant.
void pointer_to_constant(){

    /*  The value of the object (entity) pointed by pointer ptr cannot be
    changed. The read-write area stores the pointer (stack in the present case). The pointed object could be in a read-only or read-write area. */
    int i = 10;  
    int j = 20;
   
    const int *ptr = &i; /* ptr is pointer to constant */
 
    printf("ptr: %d\n", *ptr);
     
    *ptr = 100; /* error: object pointed cannot be modified
    using the pointer ptr */  
 
    ptr = &j; /* valid */
    printf("ptr: %d\n", *ptr);
}

int main(void){
    pointer_to_constant();
    return 0;
}

Output

cf.cpp: In function ‘void pointer_to_constant()’:
cf.cpp:18:10: error: assignment of read-only location ‘* ptr’
   18 |     *ptr = 100; // error: object pointed cannot be modified
      |     ~~~~~^~~~~

 

You can also read about the jump statement, Tribonacci Series

Must Read Passing Arrays to Function in C

Constant Pointer to constant

Declaration

const int *const ptr;  

Here a constant pointer is pointing to a constant integer. So here, neither we can change the Pointer to point to any other variable, nor we can change the value of the variable being pointed.

Example

#include <stdio.h>

// constant pointer to constant
void constantPointer_to_constant(){
    /* We can't change the value pointed by the pointer or point it to other
    variables because it's a constant pointer to a constant variable. */
    int i = 10;
    int j = 20;
   
    const int *const ptr = &i; /* constant pointer to constant integer */
 
    printf("ptr: %d\n", *ptr);
 
    ptr = &j;     /* error */
    *ptr = 100;   /* error */
}

int main(void){
    constantPointer_to_constant();
    return 0;
}

Output

cf.cpp: In function ‘void constantPointer_to_constant()’:
cf.cpp:15:9: error: assignment of read-only variable ‘ptr’
   15 |     ptr = &j;     /* error */
      |     ~~~~^~~~
cf.cpp:16:10: error: assignment of read-only location ‘*(const int*)ptr’
   16 |     *ptr = 100;   /* error */
      |     ~~~~~^~~~~


Also see, Short int in C Programming

Must Read Decision Making in C

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

What is a Preprocessor?

A preprocessor is software that processes a source file before submitting it to be compiled. The preprocessor allows header file inclusion, macro expansions, conditional compilation, and line control.

What is a pointer in C?

A pointer is a variable that saves or points to the address of another variable. A regular variable stores the value of a variable, but a pointer variable stores the address of a variable.

What is the difference between “const char* p” and “char const* p” ?

Here “const char* p” is a pointer to const char and “char const* p” is a pointer to a char const. So both are same.

What is meant by a document database? What are the advantages of macro over function?

Macro on a high level copy-paste, its definitions to wherever it is called. As a result, it saves a lot of time because the control is always with the callee function and no time is spent passing it to a new function. However, one disadvantage is that the built binary is enormous in size, but once compiled, the application runs much faster.

Conclusion

Cheers if you reached here!! 

This blog extensively discussed different types of database design choices, namely, document, graph and columnar databases. We also discussed an example database of each type of database with corresponding features.

We hope that this blog has helped you enhance your knowledge regarding Const Qualifier in C and if you would like to learn more, check out our articles on Databases(DBMS). Do upvote our blog to help other ninjas grow. 

Recommended Reads: Bit stuffing program in c

If you want to explore preparation strategy for SDE placements, please have a look at this YouTube tutorial.

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.!

You can follow these links to get hands-on various state of the art topics - SQL ProblemsCoding InterviewsVideo Resources. Also, do check out our course on C++.

Happy Coding!

Topics covered
1.
Introduction
2.
Const Qualifier in C
2.1.
Pointer to constant
2.2.
Constant Pointer to constant
3.
FAQs
3.1.
What is a Preprocessor?
3.2.
What is a pointer in C?
3.3.
What is the difference between “const char* p” and “char const* p” ?
3.4.
What is meant by a document database? What are the advantages of macro over function?
4.
Conclusion