There are two ways to represent these constants:
- Const keyword
- #define preprocessor
To work on the constants, we have to first mount some C language compiler on the Linux operating system. So, we have been using the “GCC” C language compiler via apt command. Open the terminal via shortcut key “Ctrl+Alt+T” after logging in to the system. Execute the stated-below query to install “GCC”.
After mounting the “GCC” compiler, we are now ready to work on constants. The “const” keyword will be used to specify a variable as constant, which means the value of this constant cannot be changed. So after opening the terminal, create a new C type file test.c using the Nano editor command as follows:
This query will open a nano editor with the name of a file specified at the top. Now we have to use the below code in our file. This code has one header file and one main function. The main function contains one float type variable “val” with a value “3.22”. This variable has been specified as a constant variable using the “const” keyword at the start of it. Then a print statement has been used to print the value of a constant variable “val” and the main function will be closed. The general syntax for the specification of constant variable is stated as follows:
Const variable-type variable-name = variable-value;
After saving the file using “Ctrl+S”, quit the file using the “Ctrl+X” shortcut key. Now we must first compile the above code using the “gcc” compiler. Therefore, write out the below query to do so along with the name of a file.
To see the output for this C language script, you must try out the “a.out” instruction as follows:
Now let’s try to change the value of the constant variable “val” within the code. For that, open the file using the “nano” keyword.
Let’s update the value of a variable “val” by assigning “5.8” at the next line. All the other lines of code will remain the same. Press Ctrl+S and Ctrl+X to save and quit the file, respectively.
Let’s compile the test.c file again using the “gcc” compilation command below. You will get an error that “assignment of the read-only variable”. This means you cannot alter the value of an already defined constant variable.
When we try out to execute the file again, it will output the same old value of variable “val” because of the “const” keyword with it and will not update it with the new one.
Another way of defining variables as constant is using the “#define” preprocessor. To specify constant or micro substitute, we should cast off #define preprocessor code directive. Every simple data type could be used. The general syntax for the #define preprocessor is as follows:
So, let’s have a simple example of #define in our Ubuntu 20.04 Linux system. Open the command shell and write the following command to create a new nano editor file to write the C program:
We have to create the same code as shown in the presented image below. This C program script contains one header and one main method. We have used the #define preprocessor variable “val” with some value “3.22” before the main method and after the header file. The main function contains one print statement showing the value of the constant variable “val”. Save and then close the file using keyboard shortcut keys.
It’s time to compile the saved code. Use the “gcc” old query for this as stated below.
We have to run the code using the “a.out” instruction. The output of the code shows the constants variable value along with some sentences
Let’s have another example for the constant variable using #define preprocessor. For this purpose, open the “test.c” file using the “nano” command in the shell.
This time we have tried a new way to understand #define. We have specified #define while using two variables, “x” and “y”, within the MAX function to check which one of these variables has a greater value. We have also used the expression to perform the MAX function as “((x)>(y)? (x): (y))”. After this, the main function with no return type has been specified. This main function contains a single print statement, which will take both numbers in parenthesis of the MAX function and let us know about the greater one.
After saving and closing this file with keyboard shortcut keys, let’s compile the test.c file with a “gcc” compiler query in the terminal shell as stated below:
To check the output for the above code, we have to run the compiled file test.c. For this objective, we will be using the “a.out” query as stated below. The output in the snapshot is showing that this program can take constant values and decide which one of the variable values is greater, e.g., 67.
At last, we have done a great job by covering two ways to represent constants in our guide, e.g., “const” keyword and “#define” preprocessor. We believe this article will be enough to understand constants completely.