Linux C Programming Tutorial Part 11 - Arithmetic, Relational, and Logical operators

On this page

  1. Conclusion

Up until now, in this C programming tutorial series, we have discussed basic things like functions, arrays, variables, and more. Continuing with the flow, in this tutorial, we will discuss another such basic concept: operators.

As a beginner in C programming language, you'll majorly deal with three kinds of operators: arithmetic, relational, and logical. Let's begin with the airthmetic operators.

There are essentially 5 types of arithmetic operators in C: +, -, *, /, and %. While +, -, and / are self-explanatory, * refers to multiplication and % is modulus operator. In case you aren't aware, the % operator gives you a remainder. For example:

a % b

Here, the % operator makes sure you get the remainder value when 'a' is divided by 'b.' This means the remainder can be zero in cases 'a' is completely divisible by 'b'. Here's a small example that should give you better clarity on these operators:

#include <stdio.h>

int main()
{
int a = 10, b = 3, c =0;
c = a + b;
printf("%d\n", c);

c = a - b;
printf("%d\n", c);

c = a*b;
printf("%d\n", c);

c = a/b;
printf("%d\n", c);

c = a%b;
printf("%d\n", c);

return 0;
}

Following is the output of this program:

13 
7
30
3
1

It's worth mentioning here that when dealing with floats and doubles, you should not use the % operator.  Also, the + and - operators we've used here are binary operators, meaning they require two operands (for example, 'a' and 'b' in our case). There are also unary + and - operators, which work on a single operand.

Here's an example of the unary - operator in action:

#include <stdio.h>

int main()
{
int a = 10, b = 3, c =0;
c = -a;
printf("c = %d", c);
}

Here's the output:

c = -10

Among all operators discussed so far, precedence wise, unary + and - share the top spot, followed by *, / and %, that are in turn followed by binary + and -. 

Please note that there are some other unary operators as well, some of which we have already discussed indirectly in our previous tutorials. Here's the list: ++, --, !, &, and sizeof.

We've already discussed the increment and decrement operators ++ and -- (in both prefix and postfix forms). ! is NOT operator which negates anything on which it is applied. For example, if the outcome of a condition is true, applying ! will convert it into false, and vice versa.

Moving on, & is used to fetch address of a variable (as already seen in case of scanf() function arguments), while sizeof operator gives you size of the operand passed to it. The following piece of code should give you a better idea about these operators:

#include <stdio.h>

int main()
{
int a = 10, b = 3, c =0, d =1, e=0, f=9;

printf("a = 10 and a++ = %d\n", a++);

printf("b = 3 and ++b = %d\n", ++b);

printf("c = 0 and c-- = %d\n", c--);

printf("d = 1 and --d = %d\n", --d);

if(!e)
{
printf("\n e is zero or FALSE and its address is: %u\n", &e);
printf("\n sizeof 'f' is: %u\n", sizeof(f));

}


return 0;

}

And here's the output:

a = 10 and a++ = 10 
b = 3 and ++b = 4
c = 0 and c-- = 0
d = 1 and --d = 0

e is zero or FALSE and its address is: 856178696

sizeof 'f' is: 4

Moving on, let's quickly take a look at relational and logical operators. Following are relational operators:

> >= < <= == !=

Here are how they can be used:

a > b
a >= b
a < b
a <= b
a == b
a != b

In order of their appearance above, these operators check whether 'a' is greater, greater than or equal to, less than, less than or equal to, equal, and not equal to 'b'. The first four operators have the same precedence, which is higher than the last two. Note that the last two operators are also called equality operators.

And finally, coming to logical operators. There are essentially two: && and ||. Both these operators are mostly used to evaluate conditions or expressions. For example:

if (cond1 && cond2)
{

}

if (expr1 && expr2)
{

}

if (cond1 || cond2)
{

}

if (expr1 || expr2)
{

}

In case of the first two if statements, the execution will only enter the block if both conditions are true. Whereas in case of last two if statements, the execution will enter the block if any of the condition of expression is true.

Keep in mind that relational operators have lower precedence than arithmetic operators, and that of logical operators is lower than relational and equality operators. Among themselves, && has higher precedence than ||.

Conclusion

In this tutorial, we discussed the basics of operators, how they work and their precedence. It's suggested you create some sample C programs to test out the concepts discussed here. In case of any doubt or query, let us know in comments below.

Share this page:

0 Comment(s)