 # Operators in C

Operators are symbol which tells the compiler to perform certain operations on variables. For example, (*) is an operator which is used for multiplying two numbers.

There are different types of operators in C. Let's take a look at each type of them with few examples of each.

• Arithmetic Operators
• Relational Operators
• Increment and Decrement Operators
• Logical Operators
• Assignment Operators

## Arithmetic Operations

Arithmetic Operators are the type of operators which take numerical values (either literals or variables) as their operands and return a single numerical value.

Let's assume, 'a' is 8 and 'b' is 4.

Operator Description Example
- Subtracts second operand from the first a-b=4
* Multiplies both operands a*b=32
/ Divides numerator by denominator. a/b=2
% Modulus Operator returns the remainder after an integer division. a%b=0

```#include <stdio.h>
int main()
{
int a = 25;
int b = 8;
printf("sum = %d", (a+b));
printf("\ndifference = %d", (a-b));
printf("\nproduct = %d", (a*b));
printf("\nremainder = %d\n", (a%b));
return 0;
}
```
sum = 33
difference = 17
product = 200
remainder = 1

If we divide two integers, the result will be an integer.

5/2=2  (Not 2.5)

To get 2.5, at least one of the numerator or denominator must have a decimal(float) value.

5.0/2=2.5     or     5/2.0=2.5     or     5.0/2.0=2.5     but     5/2 = 2.

```#include <stdio.h>
int main()
{
int a ;
int b ;
a = 2 ;
b = 9 ;
printf ( "Value of b/a is : %d\n" , b/a ) ;
return 0;
}
```
Value of b/a is : 4
```#include <stdio.h>
int main()
{
float a ;
int b ;
a = 2.0 ;
b = 9 ;
printf ( "Value of b/a is : %f\n" , b/a ) ;
return 0;
}
```
Value of b/a is : 4.500000

As we saw, if 'b' and 'a' are both integers, then the result is 4 (not 4.5) but when one of them is float then the result is 4.500000 (a float).

## Hierarchy Of Operations

Suppose, you have used more than one operator in an expression e.g.- `2*5%6/2`, then the order of execution i.e., which operator will be executed first is decided by the precedence table given below.

In maths, you might have learned about BODMAS rule, but that rule is not applied here. If we have written more than one operation in one line, then which operation should be done first is governed by following rules :-
Expressions inside brackets '()' are evaluated first. After that, this table is followed:

Category with priority Operator Associativity
1st Multiplicative * / % Left to right
2nd Additive + - Left to right
3rd Equality = Right to Left

If two operators are of same priority, then evaluation starts from left or right as stated in the table.
eg.-
k = 8/4+3-5*6+8
Solving, '*' and '/' first from left to right.
k = 2+3-30+8
Now solving, '+' and '-' from left to right
k = -17
Now solving '=' from right to left

If you don't want to remember these rules, then just put the expression you want to execute first in brackets. Eg- If you want to divide the product of (2+2) and 444 by quotient of (999/5) then write the expression as - ((2+2)*444)/(999/5). This will get evaluated as (4*444)/(999/5) and finally get simplified to 1776/199 (as 999/5 is 199 and not 199.8).

## Relational Operators

Relational Operators check the relationship between two operands. If the relationship is true, it returns 1, if the relationship is false, it returns 0.

Following is the list of relational operators in C.

Again assume the value of 'a' to be 8 and that of 'b' to be 4.

Operator Description Example
== Equal to (a == b) is false
!= Not equal to (a != b) is true
> Greater than (a > b) is true
< Less than (a < b) is false
>= Greater than or equal to (a >= b) is true
<= Less than or equal to (a <= b) is false

Let's see an example to see their use.

```#include <stdio.h>
int main()
{
int a = 5, b = 6;
printf("\n%d", a == b);
printf("\n%d", a != b);
printf("\n%d", a > b);
printf("\n%d", a < b);
printf("\n%d", a >= b);
printf("\n%d", a <= b);
return 0;
}
```
0
1
0
1
0

When the expression was true, we got 1 and when false, 0.

## Difference between = and ==

Although = and == seem to be same, but they are quite different from each other. = is assignment operator while == is the equality operator. = is used to assign values whereas == is used for comparing values.

Take two examples.

x = 5;

x == 5;

By writing x = 5, we assign a value of 5 to x, whereas by writing x == 5, we check if the value of x is 5 or not. ## Logical Operators

First, let's learn about AND and OR.

AND and OR are very much similar to English words 'and' and 'or'.

In English,

A and B - Both A and B.

A or B - Either A or B.

In C programming,

A and B - Both A and B.

A or B - Either A or B or both.

So, if we are writing A and B, then the expression is true if both A and B are true. Whereas, if we are writing A or B, then the expression is true if either A or B or both are true.

The symbol for AND is && while that of OR is ||.

Again assume the value of 'a' to be 8 and that of 'b' to be 4.

Operator Description Example
&& Logical AND. If both the operands are non-zero, then the condition becomes true (a == b) && (a==8) is false
|| Logical OR. If any one or both the operands are non-zero, then the condition becomes true (a != b) || (a==5) is true
! Logical NOT. It is used to reverse the condition. So, if a condition is true, ! makes it false. !(a==5) is true

```#include <stdio.h>
int main()
{
int a = 5, b = 0;
printf("\n%d", a && b);
printf("\n%d", a || b);
printf("\n%d", !a);
printf("\n%d", !b);
return 0;
}
```
0
1
0
1

Since a is non-zero but b is zero, so AND between them will be false (or 0). As only one of them is true (or non-zero). But with OR, since anyone of them (i.e. a) is non-zero, so, a||b is true (or 1 ).

In this example, since the value of 'a' is non-zero, therefore it is true. So, !a makes it false. The case with !b is the opposite.

## Assignment Operators

Assignment Operators are used to assign values of the operand on the right to the operand on the left and not vice versa. The most common assignment operator is =.

If we write `a = 10;`, it means that we are assigning a value '10' to the variable 'a'.

There are more assignment operators which are listed as follows.

Operator Description Example
= Assigns value of right operand to the left operand C = A+B is same as C = A + B
+= Adds the value of right operand to the left operand and assigns the final value to the left operand C += A is same as C = C + A
-= Subtracts the value of right operand to the left operand and assigns the final value to the left operand C -= A is same as C = C - A
*= Multiplies the value of right operand to the left operand and assigns the final value to the left operand C *= A is same as C = C * A
/= Divides the value of left operand from the right operand and assigns the final value to the left operand C /= A is same as C = C / A
%= takes modulus using two operands and assigns the result to the left operand C %= A is same as C = C % A
We cannot assign any value to a constant. For example, 4 = c will give an error.

`'='` operator starts from right. eg.- if a is 4 and b is 5, then `a = b` will make a equal to 5 and b will remain 5.

`a = a+b;` → Similarly, since `+` has higher priority, so, `a+b` will be calculated first (i.e. 9), then expression will become `a = 9;`. So, `a = a+b;` will make 'a' equal to 9 and b will remain 5.

Since `a += b` is same as `a = a+b`, `a += b` will also make the value of 'a' equal to 9.

```#include <stdio.h>
int main()
{
int a = 25;
int b = 8;
a += b;
printf("%d\n",a);
a -=b;
printf("%d\n",a);
a *=b;
printf("%d\n",a);
a /=b;
printf("%d\n",a);
a %=b;
printf("%d\n",a);
return 0;
}
```
33
25
200
25
1

Initially 'a' is 25 and 'b' is 8.

`a += b;` → It means `a = a+b`. Now, 'a' is `a+b` i.e. 33. (`a = a+b`)

`a -= b` → Now, 'a' is 33. So, `a -= b` (`a = a-b`) is 25. So, new 'a' is 25. Similarly, the values will be evaluated for the next three cases.

## Increment and Decrement Operators

`++` and `--` are called increment and decrement operators respectively.

++ adds 1 to the operand whereas -- subtracts 1 from the operand.

a++ increases the value of a variable 'a' by 1 and a-- decreases the value of a by 1.

Similarly, ++a increases the value of 'a' by 1 and --a decreases the value of a by 1.

In a++ and a--, ++ and -- are used as postfix whereas in ++a and --a, ++ and -- are used as prefix.

For example, suppose the value of a is 5, then both a++ and ++a changes the value of 'a' to 6. Similarly, both a-- and --a changes the value of 'a' to 4.

### Difference between Prefix and Postfix

While both `a++` and `++a` increases the value of 'a', the only difference between these is that `a++` returns the value of 'a' before the value of 'a' is incremented and ++a first increases the value of 'a' by 1 and then returns the incremented value of 'a'.

Similarly, `a--` first returns the value of 'a' and then decreases its value by 1 and `--a` first decreases the value of 'a' by 1 and then returns the decreased value.

An example will make the difference clear.

```#include <stdio.h>
int main()
{
int a = 15, b = 15, c = 15, d = 15;
printf("\nvalue of a++ = %d", a++);
printf("\nvalue of ++b = %d", ++b);
printf("\nvalue of c-- = %d", c--);
printf("\nvalue of --d = %d", --d);
return 0;
}
```
value of a++ = 15
value of ++b = 16
value of c-- = 15
value of --d = 14

In `a++` 15 is printed. Then the value of a will become 16.
And in ++b, value is first increased to 16 and then printed. Similar with `c--` and `--d`.

## sizeof

`sizeof()` operator is used to return the size of a variable. Suppose we have an integer variable 'i', so the value of `sizeof(i)` will be 4 because on declaring the variable 'i' as of type integer, the size of the variable becomes 4 bytes.

Look at the following example to find the size of int, char, float and double variables

```#include <stdio.h>
int main()
{
int i = 6;
int j;
char c;
float f;
double d;
printf("size of integer variable i = %d", sizeof(i));
printf("\nsize of integer variable j = %d", sizeof(j));
printf("\nsize of character variable = %d", sizeof(c));
printf("\nsize of float variable = %d", sizeof(f));
printf("\nsize of double variable = %d", sizeof(d));
return 0;
}
```
size of integer variable i = 4
size of integer variable j = 4
size of character variable = 1
size of float variable = 4
size of double variable = 8

Here, sizes of character, float and double variables are 1, 4 and 8 bytes respectively, so `sizeof` operator applied to these returns 1, 4 and 8 respectively. Whenever we declare an integer variable, a space in the memory equal to 4 bytes gets occupied by it. It doesn't matter whether we assign a value to the variable or not, space will allocate. Since, both 'i' and 'j' are integer variables, therefore the sizes of both of these are 4 bytes, regardless of whether a value is assigned to these or not.

Size of the variables will be different for 32 bit and 64 bit compiler.

Let's see another example.

```#include <stdio.h>
int main()
{
printf("%d", sizeof(int));
printf("\n%d", sizeof(char));
printf("\n%d", sizeof(short));
printf("\n%d", sizeof(long));
return 0;
}
```
4
1
2
8

Here, we printed the sizes of int, char, short and long using sizeof operator, same as we did in the previous example, except for the difference that this time we directly passed the name of the data type in the `sizeof` operator.

So, anytime you need to know the size of any data type, you can do so by using sizeof operator.

A lot of things are discussed here. So, solve questions from the practice section to have good command over them.
To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.
Don't practice until you get it right. Practice until you can't get it wrong.