# C Operators

In this tutorial, you will learn about different type of operators in C Language with the help of examples.

## Operators

C offers us a wide variety of operators that we can use to operate on data.

In C language, operators is a symbol which is used to perform a specific mathematical or logical functions.

In particular, we can identify various groups of operators:

**Types of Operators in C language**

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

## Arithmetic Operators

An arithmetic operator performs basic mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).

Assume variable `A = 10` and variable `B = 5` in the examples of table given below :

Operator | Meaning of Operator | Example |
---|---|---|

+ | Add two operands | A + B = 15 |

- | Subtract second operand from the first operand. | A - B = 5 |

* | Multiplies both operands | A * B = 50 |

/ | Divides numerator by denumerator | A / B = 2 |

% | Give remainder after division | A % B = 0 |

### Example : Arithmetic Operators

`#include <stdio.h>`

int main(){

int a = 10, b = 6, c;

c = a + b;

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

c = a - b;

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

c = a * b;

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

c = a / b;

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

c = a % b;

printf("Remainder when a divided by b = %d \n", c); // Modulus Operator

return 0;

}

**Output**

`a + b = 16 `

a - b = 4

a * b = 60

a / b = 1

a % b = 4

In the above example,

we observed that 10 /6 = 1 but in normal calculation 10 / 6 = 1.67 while the output in the program is 1. It is because both the variables ` a ` and ` b ` are integers . Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows answer 1 instead of 1.67.

**Note :** The Modulus operator ` % ` can only be used with integers.

## Increment and Decrement Operators

In C programming, Increment and decrement operators are used to change the value of operand by 1 .

Increment operator ` ++ ` increase the value of operand by 1 .

Decrement operator ` -- ` decrease the value of operand by 1 .

**Note :** Increment and Decrement operator are unary operator i.e. they can only be used/operate in a single operand.

These operators can be used as suffix and prefix both.

### Example : Increment and Decrement Operators

#include <stdio.h>

int main(){

int a = 5, b = 100;

float c = 5.3, d = 100.8;

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

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

printf("--c = %f \n", --c);

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

return 0;

}

**Output**

`++a is 6 `

b++ is 100

--c is 4.300000

d-- is 100.800000

**++ and -- operator as prefix and postfix**

In the above example,

++ operator is used as prefix with variable a i.e. the value of variable a is incremented by 1 then, it returns the value.

++ operator is used as postfix with variable b i.e. the original value of variable a is returned first then, the value of b is incremented by 1.

-- operator is used as prefix with variable c i.e. the value of variable c is decremented by 1 then, it returns the value.

-- operator is used as postfix with variable d i.e. the original value of variable d is returned first then, the value of d is incremented by 1.

## Assignment Operators

The assignment operators are used for assigning a value to a variable.

Operator | Example | Same as |
---|---|---|

= | a += b | a = a + b |

-= | a -= b | a = a - b |

*= | a *= b | a = a * b |

/= | a /= b | a = a / b |

%= | a %= b | a = a % b |

### Example : Assignment Operators

`#include <stdio.h>`

int main(){

int a = 10, b;

b = a; // Here, b is 10

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

b += a; // Here, b is 20

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

b -= a; // Here, b is 10

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

b *= a; // Here, b is 100

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

b /= a; // Here, b is 10

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

b %= a; // Here, b = 0

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

return 0;

}

**Output**

b = 10

b = 20

b = 10

b = 100

b = 10

b = 0

### Relational Operators

Relational operators are used to check/define the relationship between two entities. If the relation is true, it return 1; if the relation is false, it return 0.

**Note :** Relational operators are generally used in loops and decision making.

Now, Assume `a = 5` for examples in the table given below :

Operator | Meaning of Operator | Example |
---|---|---|

Operator | Meaning of operator | Example |

== | Equal to | a == 5 |

> | Greater than | a > 2 |

< | Less than | a < 8 |

!= | Not equal to | a != 4 |

>= | Greater than or equal to | a >= 1 |

<= | Less than or equal to | a <= 6 |

### Example : Relational Operators

`#include <stdio.h>`

int main(){

int a = 10, b = 5, c = 10;

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

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

printf("%d > %d is %d \n", a, b, a > b);

printf("%d > %d is %d \n", a, c, a > c);

printf("%d < %d is %d \n", a, b, a < b);

printf("%d < %d is %d \n", a, c, a < c);

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

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

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

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

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

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

return 0;

}

**Output**

10 == 5 is 0

10 == 10 is 1

10 > 5 is 1

10 > 10 is 0

10 < 5 is 0

10 < 10 is 0

10 != 5 is 1

10 != 10 is 0

10 >= 5 is 1

10 >= 10 is 1

10 <= 5 is 0

10 <= 10 is 1

### Logical Operators

Logical operators are used to make decision (True or False) depending on the given expression.

These operators are commonly used in decision making in C programming.

Operator | Meaning | Example |
---|---|---|

Operator | Name | Meaning |

&& | Logical AND | True only if all the operands are True |

|| | Logical OR | True only of either one operand is true |

! | Logical NOT | True only if the operand is 0 |

### Example : Logical Operators

`#include <stdio.h>`

int main(){

int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);

printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);

printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);

printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);

printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);

printf("!(a != b) is %d \n", result);

result = !(a == b);

printf("!(a == b) is %d \n", result);

return 0;

}

**Output**

(a == b) && (c > b) is 1

(a == b) && (c < b) is 0

(a == b) || (c < b) is 1

(a != b) || (c < b) is 0

!(a != b) is 1

!(a == b) is 0

### Bitwise Operators

Bitwise operators are special operator set provided in C programming. They are used to convert operands to bit level for basic computation which makes processing faster.

**Note :**Bitwise Operators are very helpful in programming where we have limited amount of memory and want to run program faster.

Operators | Meaning of operators | Working |
---|---|---|

& | Bitwise AND | Give result 1 (true) if both bits are 1 (true) |

| | Bitwise OR | Give result 1 (true) if any of the two bits is 1 (true) |

^ | Bitwise XOR | Give result 1 (true) if the two bits are different |

<< | Left Shift | Left shifts the bits of the first operand, the second operand decides the number of places to shift |

>> | Right Shift | Right shifts the bit of the first operand, the second operand decides the number of places to shift |

~ | Bitwise NOT | Invert all bits of operand |

### Example : Bitwise Operator

`#include <stdio.h>`

int main(){

unsigned char a = 5, b = 9;

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

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

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

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

printf("b << 1 = %d \n", b << 1);

printf("b >> 1 = %d \n", b >> 1);

return 0;

}

**Output**

a & b = 1

a | b = 13

a ^ b = 12

~a = 250

b << 1 = 18

b >> 1 = 4

To know more about Bitwise operators Click here.

## Other Operators

### The ternary operator

The ternary operator is the only operator in C that works with 3 operands, and it’s a short way to express conditionals.

This is how it looks:

`condition ? expression : expression`

Example:

`a ? b : c`

If `a`

is evaluated to `true`

, then the `b`

statement is executed, otherwise `c`

is.

The ternary operator is functionality-wise same as an if/else conditional, except it is shorter to express and it can be inlined into an expression.

### Comma Operator

Comma operator is used to link related expressions together or seperate two operands. For example:

`int a = 2, c = 3, d = 4;`

### The sizeof operator

The `sizeof` operator is the unary operator in C used to compute the size of its operand i.e. it return the size of a variable.

In simple words, the `sizeof`

operator returns the size of the operand you pass. You can pass a variable, or even a type.

**Example : sizeof Operator**

`#include <stdio.h>`

int main(){

int a;

float b;

double c;

char d;

printf("Size of int=%lu bytes\n", sizeof(a));

printf("Size of float=%lu bytes\n", sizeof(b));

printf("Size of double=%lu bytes\n", sizeof(c));

printf("Size of char=%lu byte\n",sizeof(d));

return 0;

}

**Output**

Size of int = 4 bytes

Size of float = 4 bytes

Size of double = 8 bytes

Size of char = 1 byte

**Note :** The size of data types are different in different systems.

We will learn about the remaining operators in later tutorials.

## Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Operators in C.

**Keep Learning : )**

In the next tutorial, you'll learn about C `If else`

.