‘const’ type qualifier
Type qualifiers in ‘C’
There are two important type qualifiers in ‘C’.
- const
- volatile
Applying these qualifiers to a variable declarations is called qualifying the declarations.
These are very important qualifiers you should understand to write proper and bug free code for your embedded device.
Definition of ‘qualifier’:
The English meaning of the word ‘qualifier‘ is a word or phrase, especially an adjective, used to attribute a quality to another word, especially a noun.
In programming, also the same case. We use ‘const’ and ‘volatile’ qualifiers to attribute a feature to a variable.
‘const’ type qualifier:
‘const’ is a type qualifier in ‘C’ used to enforce read-only features on variables. When you define a variable as ‘const,’ it becomes a read-only variable, meaning its value cannot be modified after initialization.
Consider the following example:
Now consider uint8_t data1 =10 variable definition. Here, ‘data1’ is a variable of type uint8 and that is initialized to 10. And in the program, you can modify the value of that variable, which is allowed. So, the ‘data1’ value can be modified throughout the program. No issues with that.
But when you use ‘const’ qualifier with a variable definition, then this variable changes, it becomes read-only. uint8_t const data2 =10; Here ‘data2’ is called a constant variable or read-only variable.
And in the program, if you try to change the value of the data2 variable, then this will trigger a compilation error. This is not allowed. So, you have to initialize that variable when you define it. After that, you are not allowed to change the value of that variable. Because now it is a read-only variable.
Let’s analyze the below statement.
Here uint8_t is the data type, we also call it as type specifier of a variable. ‘const’ is a type qualifier of a variable. And data1 is a variable and in this case, it is a read-only variable. And here the value of the data1 is 10 and it is fixed at 10. The value of the data1 can’t be modified.
Let’s understand the syntax of how you should use this ‘const’ keyword in the variable definition.
There are two possible ways.
- You can either write it like const uint8_t data1 = 10;
That means you first start with a ‘const’ that is a type qualifier, followed by a type specifier, followed by a variable name. You can do it like this, no problem.
- Or you can also do like uint8_t const data1 = 10;
First start with the type specifier, followed by the type qualifier that is ‘const’, and then the variable name.
So, the ‘const’ keyword can appear before the type specifier or after the type specifier. Both are allowed. That’s why, both statements are identical, no issues with that.
But, I usually prefer the second method, that is first you mentioned the data type which is the type specifier, and then the type qualifier. Because it helps you to understand complex statements better.
Here, if you want to understand or convert uint8_t const data1 = 10 this statement into English, then you should first identify the variable name. data1 is a variable name and then moves to your left-hand side. So, this is how you should read it. Data1 is a constant variable of type uint8. So, that’s how you should read it. So, it helps you to read the statement better, that’s why always start with the type specifier.
About “const”ness of a variable
- By using the ‘const’ keyword, you are just making a promise to the compiler that you(the programmer) won’t try to modify the content of the variable using its name.
- If you try to modify the variable by its name, the compiler stops you by throwing an error.(compile time error)
- You can still modify the content of the variable by using its address.
‘const’ variable means that you cannot change the value by its name, but you can change the value of that variable by its address.
Let’s look at the below small program to understand the constants of a variable.
#include <stdio.h> #include <stdint.h> int main(void) { uint8_t const data = 10; data = 50; printf("Value = %u\n", data); getchar(); }
Here, variable of type uint8_t data = 10; Let me make this read-only. So, I introduce a ‘const‘ keyword here. I try to change the value of data = 50. Then print the value of that variable.
When I compile this code, it gives an error. The error says that, assignment of read-only variable ‘data’.
So, now data =50 has been converted into a read-only variable, that’s why the compiler is saying that this is not allowed.
But, still I can change the value of the data = 50. So, I simply create a pointer ‘ptr’ and I initialize the pointer variable with the address of the variable. Then, I write whatever value I like into the address pointed by ptr. That’s why *ptr = 50. After that print the value here.
#include <stdio.h> #include <stdint.h> int main(void) { uint8_t const data = 10; printf("Value = %u\n",data); uint8_t *ptr = (uint8_t*)&data; //uint8_t const * *ptr = 50; printf("Value = %u\n",data); getchar(); }
You should remember that ‘const‘ doesn’t mean that the value never changes, it’s only a programming safety feature to ensure that the programmer shouldn’t try to modify the value.
Because the compiler alerts you if anyone tries to change the value of that variable by its name. That’s why ‘const’ is used as a programming safety feature.
Conclusion:
In summary, the ‘const’ type qualifier in C is a valuable tool for indicating that a variable’s value should not be modified directly. It serves as a safety feature to catch attempts to modify the variable’s value by its name during compilation, making code more robust and reliable.
In the following article, let’s understand the placement of ‘const’ variables in memory.
Get the Full Course on Microcontroller Embedded C programming Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1