Operator precedence in C or any other programming language is defined as the order of priority according to which different operators are executed within arithmetic or logical expression. Whenever we intend to make a decision in a C program or perform a calculation, the operators and their associated expressions are used very extensively.
In this guide, we will educate you on the precedence of these operators in the C programming language in Linux by demonstrating some relevant examples.
Precedence of Operators in C:
The general precedence of the most commonly used arithmetic and logical operators in C programming language in Linux is as follows:
BODMAS rule is applied on the arithmetic operators, i.e., in any given arithmetic expression, brackets are evaluated first, followed by orders (powers), division, multiplication, addition, and subtraction in the specified sequence, respectively.
Bit-wise AND operator (&) is evaluated first followed by bit-wise OR (|), logical AND (&&), and logical OR (||) in the specified sequence, respectively.
Examples of Operator Precedence in C in Linux:
In the following examples, we will try to throw light on a very interesting phenomenon, i.e., how operator precedence affects the results of a calculation.
We have devised the first example by creating a C file on our Linux Mint 20 system named “Operator.c”. In this file, we have written a C program that performs a simple arithmetic calculation on four different variables and stores the result in the fifth variable. We have initialized 4 variables with values 10, 8, 6, and 4, respectively. In our arithmetic expression, we wanted to perform addition, multiplication, and division of the said variables. However, you can see from the C program shown in the image below that in the given expression, we are not using any brackets. Because of this, these operators will be evaluated from right to left, i.e., first, var3 will be divided by var4, then their result will be multiplied with var2, and finally, their result will be added to var1. Then we have displayed the value of the “result” variable that has stored the result of this arithmetic expression on the terminal.
After saving our code, we will compile it in our terminal with the affixed command:
If there are no errors in your C program, then it will be compiled successfully without showing any messages on the terminal.
Now, you need to run this code with the command shown below:
The result of our expression in this example is 22, as shown in the following image:
Now, we are using the same C program that we created above with a slight tweak. We have only enclosed var1 and var2 in brackets, as you can see in the image shown below. Now, since the precedence of brackets is more than any other arithmetic operators, which is why the expression enclosed within these brackets will be evaluated first, i.e., var1 and var2 will be added first. However, outside these brackets, var3 will be divided by var4 according to the precedence and finally, the result of the expression enclosed within the brackets will be multiplied with the result of the division of var3 with var4. Just like example #1, the result will be stored in the “result” variable and will be displayed on the terminal once this program is executed.
We will compile and run this C program in the same manner as we did in example #1. However, this time our output will be 27, as shown in the following image, which is clearly different from the output of the arithmetic expression evaluated in example #1.
We will now create a third variant of the very same C program that we created in the first example. We will be using the very same variables with the same operators. However, the only dissimilarity will be the placement of brackets. This time, we have enclosed var1, var2, and var3 within brackets, as shown in the image below. Now, according to operator precedence in C, the expression within these brackets will be evaluated first. If you look closely, you will notice that this time, we have two different operators within brackets, i.e., multiplication and addition. Out of these two operators, the precedence of multiplication is more than addition. That is why var2 will be multiplied with var3 first, then their result will be added to var1, and finally, the result of this whole expression will be divided with var4. The complete result will be stored in the “result” variable and will be displayed on the terminal.
Again, we will compile and run this C program in the manner described in example #1. However, because of the slight modification in our code, the output of our code this time will be 14, as shown in the following image. This output is different from the one we got in our first and second examples.
From this article, you can easily notice how the precedence of operators impacts the result of an arithmetic expression. Similarly, the operator precedence also affects the result of the logical expressions in the C programming language in Linux. You can get an even clearer understanding of operator precedence by experimenting with different logical and arithmetic operators in C while creating simple examples like the ones we discussed today. You can simply change the position of different operators and see how it changes the result of your devised expression. This understanding will not only make the concept of operator precedence strong for you but will also allow you to write usable arithmetic and logical operations in C in Linux.