C Programming

Constants in C

Constants are parameters or variables inside the C/C++ computer language that couldn’t be changed once they are specified, as the title implies. These are programmatically endogenous variables. A constant is an attribute or element in a program that cannot be modified, such as 40, 67, x, 7.4, “java programming”, and so on. In the C language, there are several kinds of constants, e.g., integer, character, string, and many more.

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”.

$ sudo apt install gcc

Const Variable:

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:

$ nano test.c

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.

$ gcc test.c

To see the output for this C language script, you must try out the “a.out” instruction as follows:

$ ./a.out

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.

$ nano test.c

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.

#Define Preprocessor:

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:

#define variable-name variable-value

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:

$ nano test.c

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.

$ gcc test.c

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

$ ./a.out

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.

$ nano test.c

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:

$ gcc test.c

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.

$ ./a.out

Conclusion:

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.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.