In this tutorial, we will discuss the different C operators with a code explanation. In a C program, the operators are a symbol that performs ** operations **on a value or in a variable.

For e.g., (-), (+), (*), and (/), are the operators to perform ** subtraction**,

*,*

**addition***and*

**multiplication,***.*

**division**Furthermore, C language has built-in operators which are the following:

- Arithmetic Operators
- Increment and Decrement Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Operators Precedence

## Arithmetic Operators in C Language with Examples

The arithmetic operator performs specific mathematical operations like multiplication, division, subtraction, and addition in numerical values such as constant and variables.

Operator Symbol | Description of Operator |
---|---|

+ | addition or unary plus |

– | subtraction or unary minus |

* | multiplication |

/ | division |

% | remainder after division or modulo division |

Let’s have a look at the example of arithmetic operators below:

```
// arithmetic operators examples
#include <stdio.h>
int main()
{
int x = 8,y = 5, z;
z = x+y;
printf("x+y = %d \n",z);
z = x-y;
printf("x-y = %d \n",z);
z = x*y;
printf("x*y = %d \n",z);
z = x/y;
printf("x/y = %d \n",z);
z = x%y;
printf("Remainder when x divided by y = %d \n",z);
return 0;
}
```

*Output:*

x+y = 13

x-y = 3

x*y = 40

x/y = 1

Remainder when x divided by y = 3

Here are the explanations of the example above.

- The (
**+**) symbol operator refers to a computation of addition. - The (
**–**) symbol operator refers to a computation of subtraction. - The (
*****) symbol operator refers to a computation of multiplication. - The (
**/**) symbol operator refers to a computation of a division. - For a normal calculation 8/5 = 1.6. On the other hand, the output to run the program is 1.
- Because both of the variables x and y are integers. However, the output result will be also an integer.
- In C program, a compiler declines the term after a decimal point and it will show the answer 1 instead of 1.6.
- The (%) module symbol operator refers to the computation of a remainder. If x=8 is divided by y=5, the remainder will be 3.
- Note that % operator will be used with integers only.

Lets have another example: We assume x = 12.0, y = 5.0, z = 10, and w = 2

```
// neither one of the operands is a floating-point number
x/y = 2.4
x/w = 6
y/x = 0.41
// Both operands are integers
z/w = 5
```

## Increment and Decrement Operators in C with Examples

In a C program, has usually two operators which are the ** increment (++)** operator and the other one is

**. When changing the value of an operand(variable or constant) by 1.**

*decrement (–)*Increment(`++`

) increases the value by 1 whereas decrement(`--`

) decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.

In addition, here are few examples below:

- ++i and i++: it means the result is i=i+1
- –k and k−−: it means the result is k=k-1.

```
// Here are the example of increment and decrement operators
#include <stdio.h>
int main()
{
int i = 13, k = 85;
float l = 9.9, m = 95.9;
printf("++i = %d \n", ++i);
printf("--k = %d \n", --k);
printf("++l = %f \n", ++l);
printf("--m = %f \n", --m);
return 0;
}
```

*Output:*

++i = 14

–k = 84

++l = 10.900000

–m = 94.900002

In a code example above, the operators (`++`

) and (`--`

) are have been used as prefixes. These two operators `a++`

and `a--`

are used for postfixes.

## Assignment Operator in C with Examples

In a C language, the assignment operator is usually used for assigning a value to a variable in a code program.

Besides, assignment operators are also applied for assigning the result of a statement to variables. The operator mostly plays an important role in assigning values to any variable. The most frequent operator is **=**.

In addition, in a C program, a collection has a compilation of a few assignment operators, which can be used for a C programming language.

In the table below, are the lists of all the assignment operators, which is supported by the C language:

Operator Symbol | Operator Description | Operator Example |
---|---|---|

= | This is for assigning | Z = X + Y it will assign a value of X + Y to Z. |

+= | This is for Adding then Assigning | Z += X is same as Z = Z + X |

-= | This is for Subtracting then Assigning | Z -= X is same as Z = Z – X |

*= | This is for Multiplying then Assigning | Z *= X is same as Z = Z * X |

/= | This is for the Divide then Assigned | Z /= X is same as Z = Z / X |

%= | This is for Modulus then Assigning | Z %= X is same as Z = Z % X |

<<= | This is for the Left shift and Assign | Z <<= 7 is same as Z = Z << 7 |

>>= | This is for Right shift and Assign | Z >>= 9 is same as Z = Z >> 9 |

&= | This is for Bitwise AND Assign | Z &= 3 is same as 3 = Z & 3 |

^= | This is used for bitwise exclusive OR and an assignment operator. | Z ^= 5 is same as Z = Z ^ 5 |

|= | This is used for bitwise inclusive OR and an assignment operator. | Z |= 4 is same as Z = Z | 4 |

**For example:**

```
// An Example of assignment operators
#include <stdio.h>
int main()
{
int x = 17, y;
y = x;
printf("y = %d\n", y);
y += x;
printf("y = %d\n", y);
y -= x;
printf("y = %d\n", y);
y *= x;
printf("y = %d\n", y);
y /= x;
printf("y = %d\n", y);
y %= x;
printf("y = %d\n", y);
return 0;
}
```

*Output:*

y = 17

y = 34

y = 17

y = 289

y = 17

y = 0

## Relational Operator in C with Examples

In a C program, the relational operator is commonly used to match two values in a program. It analyzes the relationship between the two operands.

When the given relationship is true, probably it will return 1 and when the relationship is false, then it returns 0.

Relational operators are mostly used in decision-making and loops. Let’s have a look at the table below.

Operator Symbol | Description of Operator | Example |
---|---|---|

== | It is used to determine whether the values of two operands are equal or not. When the values of the two operands are equal, then the condition will result true. | (5 == 9) is not true. |

> | It is used to determine whether the values of left operand are greater than the value of right operand. When the left operand is greater than, then the condition will result true. | (7 > 9) is not true. |

< | It is used to determine whether the values of the left operand are less than the value of right operand. When the left operand is less than, then the condition will result true. | (6 < 8) is true. |

!= | It is used to determine whether the values of two operands’ are equal or not. When the values of the two operands are not equal, then the condition will result true. | `(8 != 5) is true.` |

>= | It is used to determine whether the values of left operand are greater than or equal to the value of right operand. When a value of the left operand is greater than or equal to the value, then the condition will result true. | (4 >= 2) is not true. |

<= | It is used to determine whether the values of left operand are less than or equal to the value of right operand. When the value of the left operand is less than or equal to the value, then the condition will result true. | `(3 <= 1) is not true.` |

Let’s have a look at the example of a relational operator below

```
// Working of relational operators
#include <stdio.h>
int main()
{
int x = 8, y = 8, z = 30;
printf("%d == %d is %d \n", x, y, x == y);
printf("%d == %d is %d \n", x, z, x == z);
printf("%d > %d is %d \n", x, y, x > y);
printf("%d > %d is %d \n", x, z, x > z);
printf("%d < %d is %d \n", x, y, x < y);
printf("%d < %d is %d \n", x, z, x < z);
printf("%d != %d is %d \n", x, y, x != y);
printf("%d != %d is %d \n", x, z, x != z);
printf("%d >= %d is %d \n", x, y, x >= y);
printf("%d >= %d is %d \n", x, z, x >= z);
printf("%d <= %d is %d \n", x, y, x <= y);
printf("%d <= %d is %d \n", x, z, x <= z);
return 0;
}
```

*Output:*

8 == 8 is 1

8 == 30 is 0

8 > 8 is 0

8 > 30 is 0

8 < 8 is 0

8 < 30 is 1

8 != 8 is 0

8 != 30 is 1

8 >= 8 is 1

8 >= 30 is 0

8 <= 8 is 1

8 <= 30 is 1

## Logical Operator in C with Example

An expression containing a logical operator in C programming returns either 0 or 1. It depends upon the condition whether the expression results are either true or false.

Besides, the logical operators are usually used for decision-making in a C language.

Furthermore, there are three logical operators that we need to evaluate more than one condition to make decisions. These logical operators are the following:

- && (it means logical AND)
- || (it means logical OR)
- ! (it means logical NOT)

Operator | Name | Description | Example |
---|---|---|---|

&& | Logical AND | It will return true when both statements are true | When x = 8 and y = 4 then, statement `((x==8) && (y>8))` equals to 0. |

|| | Logical OR | It will return true when one of the statements is true | When x = 8 and y = 4 then, statement `((x==8) || (y>8))` equals to 1. |

! | Logical NOT | It will reverse the result, then if returns false if the result is true | If x = 8 then, statement `!(x==8)` equals to 0. |

Let’s have a look at the example of logical operator below.

```
// This is the examples of logical operators
#include <stdio.h>
int main()
{
int x = 12, y = 12, z = 40, output;
output = (x == y) && (z > y);
printf("(x == y) && (z > y) is %d \n", output);
output = (x == y) && (z < y);
printf("(x == y) && (z < y) is %d \n", output);
output = (x == y) || (z < y);
printf("(x == y) || (z < y) is %d \n", output);
output = (x != y) || (z < y);
printf("(x != y) || (z < y) is %d \n", output);
output = !(x != y);
printf("!(x != y) is %d \n", output);
output = !(x == y);
printf("!(x == y) is %d \n", output);
return 0;
}
```

*Output:*

(x == y) && (z > y) is 1

(x == y) && (z < y) is 0

(x == y) || (z < y) is 1

(x != y) || (z < y) is 0

!(x != y) is 1

!(x == y) is 0

The explanation of the logical operator program is above.

`(x == y) && (z > 12)`

It evaluates to 1 because both operands`(x == y)`

and`(z > y)`

is 1, then the result is true.`(x == y) && (z < y)`

It evaluates to 0 because operand`(z < y)`

is 0, then the result will be false.`(x == y) || (z < y)`

It evaluates to 1 because`(x = y)`

is 1, then the result is true.`(x != y) || (z < y)`

It evaluates to 0 because both operand`(x != y)`

and`(z < y)`

are 0, then it will result to false.`!(x != y)`

It evaluates to 1 because operand`(x != y)`

is 0, then the result is false. However, !(x != y) is 1, then it will result true.`!(x == y)`

It evaluates to 0 because`(x == y)`

is 1, then it will result true. However,`!(x == y)`

is 0, then it will result false.

## Bitwise Operators in C with Examples

In a C language, the bitwise operators are the operators which work on bits and operate the bit-by-bit operation. It performs mathematical operations such as addition, subtraction, multiplication, division, and etc.

It is changed to a bit-level, which speeds up the processing and makes it simpler to implement during computation and software compilation.

In addition, bitwise operators are commonly used in C programming for implementing bit-level operations. In the table below are the symbol and name of the operators.

Operators Symbol | Name of operators | Example of Operators |
---|---|---|

& | Bitwise AND | (X & Y) = 16, i.e., 00010000 |

| | Bitwise OR | (X | Y) = 59, i.e., 00111011 |

^ | Bitwise exclusive OR | (X ^ Y) = 43, i.e., 00101011 |

~ | Bitwise complement | (~X ) = ~(50), i.e., -0111101 |

<< | Shift left | X << 2 = 200 i.e., 11001000 |

>> | Shift right | X >> 2 = 12 i.e., 00001100 |

## The other common operators

*Comma Operator*

*Comma Operator*

The comma operators are used to link relevant statements together.

Let’s have a look at an example of comma operator:

`int x, y = 10, z;`

*The sizeof operator*

*The sizeof operator*

In C language, a is a unary operator that returned the size of data like constants, variables, array, structure, and etc.

`sizeof`

Let’s have a look at an example size of operator:

```
#include <stdio.h>
int main()
{
int x;
float y;
double z;
char d;
printf("The Size of int=%lu bytes\n",sizeof(x));
printf("The Size of float=%lu bytes\n",sizeof(y));
printf("The Size of double=%lu bytes\n",sizeof(z));
printf("The Size of char=%lu byte\n",sizeof(d));
return 0;
}
```

**Output:**

The Size of int=4 bytes

The Size of float=4 bytes

The Size of double=8 bytes

The Size of char=1 byte

## Conclusion

In this tutorial on Operators in C, we have discussed and learned almost all the Operators in C with proper explanations of examples.

The tutorials begin with a brief introduction to Operators in C followed by details of the different types of Operators in C.

Furthermore, we have given a brief overview of all the Operators in C language and explained the basic introduction of the operators like arithmetic, increment/decrement, assignment, relational, logical, bitwise, comma operator, and also the *sizeof *operator.

After the overview, we also discussed the topic with an example for an easy understanding of the topic.

The other important operators like comma and *sizeof *which are very useful in C programming language have been discussed as well.

We hope through this tutorial you can improve your knowledge of Operators in C and learn how we can use it for our software development projects.