## DEV Community

Sujith V S

Posted on • Updated on

# Operators in C programming

Operators are special symbols that is used to perform operations on values and variables.

## Arithmetic Operators

`+` Addition
Add two operands. `x + y`

`-` Subtraction
Subtract the second operand from the first operand. `x – y`

`*` Multiplication
Multiply two operands. `x * y`

`/` Division
Divide the first operand by the second operand. `x / y`

`%` Modulus
Calculate the remainder when the first operand is divided by the second operand. `x % y`

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

int main() {
int x = 12;
int result = x + 9;
printf("%d", result);
return 0;
}
``````

Adding floating point number to a whole number

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

int main() {
double x = 12.56;

int y = 8;

double result = x + y;
printf("%.2lf", result);
return 0;
}
``````

Output

``````20.56
``````

When we add a floating point number to a whole number the whole number is converted to a floating point number. The main reason for that is because of the datatype hierarchy. With datatype hierarchy the lower datatype in the hierarchy is converted to higher datatype.

Dividing two double type values
If we divide two integer numbers the result will only be the integer part(values before decimal point) of the quotient, the fractional part(values after decimal point) will be ignored. But if we divide using double or float values we will get exact result with reminder.

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

int main() {

double x = 12.00;
double result = x/8.00;

printf("%.2lf", result);

return 0;
}
``````

Output:

``````1.50
``````

Finding remainder of two values using %(Modulus) operator
The modulus operator can only be used with integer data. If we use it with floating point number it will show an error.

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

int main() {
int x = 12;
int result = x % 8;

printf("%d", result);

return 0;
}
``````

## Increment and decrement operator

Increment and decrement operators are called unary operators because it performs operation in a single operand.
`i++` - increment
`i--` - decrement
Increment and decrement operations can be done in two ways. One way is prefix and the other way is postfix.
The main difference between prefix and postfix operators is the order of evaluation. In prefix operators, the increment is performed before the value is used, while in postfix operators, the increment is performed after the value is used.

``````int i = 5;
int j = i++; // j will be equal to 5, and i will be equal to 6
int k = ++i; // k will be equal to 6, and i will also be equal to 6
``````

In the first example, the postfix increment operator (i++) is used. This means that the value of i is evaluated first, which is 5. Then, the increment operation is performed, and i is incremented to 6. However, the value of j is equal to the original value of i, which is 5.
In the second example, the prefix increment operator (++i) is used. This means that the increment operation is performed on i first, which increments it to 6. Then, the value of i is evaluated, which is also 6. Therefore, the value of k is equal to 6.

Performing operations with multiple operators

``````int main() {
int x = (4 / 2) + (6 * 5) - 1;

printf("%d", x);

return 0;
}
``````

Output:

``````31
``````

Why the order of execution is first division`/` then multiplication`*` then addition`+` and finally subtraction`-`?
It is because of operator precedence and associativity.
operators with higher precedence are executed first and operators with lower precedence are executed last.

## Comparison operators

`>` Greater than
`<` less than
`==` equal to
`>=` Greater than or equal ti
`<=` less than or equal to.

``````int main() {

int u = 78;

bool value = (u <= 15);

printf("%d", value);

return 0;
}
``````

## Logical operators

Logical operators are used with boolean expression to perform logical operations.
The three main logical operators are, `&&` `||` `!`

`&&` - AND (It returns true only if both expressions are true otherwise its false).

`||` - OR (It returns true if any one expression is true).

`!` - NOT(it reverse the result of boolian operator).

Example of && operator:

``````int main() {

int age = 26;
double height = 6.3;

bool result = (age >= 18) && (height > 6.0);

printf("%d", result);

return 0;
}
``````