Operators in C

C language offers many types of operators. Let’s take a look at each type of operators with a few examples for each.

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

Arithematic Operators in C

C supports almost common arithmetic operators such as +,-,*, / and modulus operator %. The modulus operator (%) returns the remainder of integer division calculation. Note that the modulus operator cannot be applied to a double or float.

Let’s assume ‘a’ takes value 8 and ‘b’ takes value 4.

OperatorDescriptionExample
Subtracts second operand from the firsta-b=4
*Multiplies both operandsa*b=32
/Divides numerator by denominator.a/b=2
%Modulus Operator returns the remainder of 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

Lets take an exaple in which you can easily ubnderstand all the arithematic operators

```#include<stdio.h>

void main() {
int x = 10, y = 20;

printf("x = %d\n", x);
printf("y = %d\n", y);
/* demonstrate = operator + */
y = y + x;
printf("y = y + x; y = %d\n", y);

/* demonstrate - operator */
y = y - 2;
printf("y = y - 2; y = %d\n", y);
/* demonstrate * operator */
y = y * 5;
printf("y = y * 5; y = %d\n", y);

/* demonstrate / operator */
y = y / 5;
printf("y = y / 5; y = %d\n", y);

/* demonstrate modulus operator % */
int remainder = 0;
remainder = y % 3;

printf("remainder = y %% 3; remainder = %d\n", remainder);

return 0;

```
x = 10
y = 20
y = y + x; y = 30
y = y 2; y = 28
y = y * 5; y = 140
y = y / 5; y = 28
remainder = y % 3; remainder = 1

Assignment Operators in C

Assignment Operators in c-language are used to assign values of the operand on the right to the operand on the left. The most common assignment operator is =.

If we write a = 10; means that we are assigning a value ’10’ to the variable ‘a’.

There are more assignment operators which are listed in the table bellow.

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

Lets try an example through which we can easily understand the assignment operators

```#include<stdio.h>

int main()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
}```

Total = 45

Increment and decrement Operator

In C language there are two operators for incrementing and decrementing the value of variables.

• The increment operator ++ adds 1 to its operand.
• The decrement operator — subtracts 1 from its operand

The C increment and decrement operators can be used either as prefix operator or postfix operators as follows:

```variable++;
variable--;
++variable;
--variable;```

Increment operator

The C increment operator in both prefix or postfix contexts is to add 1 to a variable. But the expression ++variable increments variable before its value is used, whereas variable++increments variable after its value has been used.

Lets take an example.

```#include<stdio.h>

void main() {
int x = 10;
int y;

/* prefix */
y = ++x;
printf("x = %d\n",x);
printf("y = %d\n",y);

x = 10;
/* postfix */
y = x++;
printf("x = %d\n",x);
printf("y = %d\n",y);

return 0;
}```

x = 11
y = 11
x = 11
y = 10

Decrement operator

1. Decrement operator is used to decrease the current value of variable by subtracting integer 1.
2. Like increment operator decrement operator can be applied to only variables.
3. Decrement operator is denoted by –.

Pre-decrement operator is used to decrement the value of variable before using in the expression. In the Pre-decrement value is first decremented and then used inside the expression.

Post-decrement operator is used to decrement the value of variable immediatly after executing expression completely in which post decrement is used. In the Post-decrement old value is first used in a expression and then old value will be decrement by

Lets take an example.

```#include<stdio.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=--i;
printf("x: %d",x);
printf("i: %d",i);
getch();
}```

x: 9
i: 9

Relational Operators

The following table

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.

OperatorDescriptionExample
==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

Lets take 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

Logical Operators

You use C logical operators to connect expressions and/or variables to form compound conditions. The C logical expression returns an integer (int). The result has value 1 if the expression is evaluated to true otherwise it returns 0. C uses the following symbols for the Boolean operations AND, OR, and NOT.

OperatorDescriptionExample
&&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>
#include <stdlib.h>

int main(int argc, char** argv) {
int x = 10;
int y = 15;
int z = 20;

if (x < y && y < z) {
printf("x > y && y < z\n");
}
if (x > y || y < z) {
printf("x > y || y < z\n");
}
if (!(x > y)) {
printf("!(x > y)\n");
}
return (0);
}```
x > y && y < z
x > y || y < z
!(x > y)