# Operators

## Operators in C

Operator is a symbol given to an operation that operates on some value. It tells the computer to perform some mathematical or logical manipulations. Such as `+` is an arithmetic operator used to add two integers or real types.

C language provides a rich set of operators. Operators are classified into following categories based on their usage.

### Various operators in C programming

Let us suppose `a = 10`, `b = 5`.

OperatorDescriptionExample
Arithmetic operator
Arithmetic operator are used to perform basic arithmetic operations.
`+`Add two integer or real type.`a + b` gives 15
`*`Multiply two integer or real types.`a * b` gives 50
`/`Divide two integer or real types.`a / b` gives 2
`%`Modulus operator divide first operand from second and returns remainder.`a % b` gives 0 (As 10/5 will have 0 remainder)
Assignment operator
Assignment operator is used to assign value to a variable. The value is assigned from right to left.
`=`Assign value from right operand to left operand.`a = 10` will assign 10 in a
Relational operators
Relational operators are used to check relation between any two operands.
`>`If value of left operand is greater than right, returns true else returns false`(a > b)` returns true
`<`If value of right operand is greater than left, returns true else returns false`(a < b)` returns false
`==`If both operands are equal returns true else false`(a == b)` returns false
`!=`If both operands are not equal returns true else false.`(a != b)` returns true
`>=`If value of left operand is greater or equal to right operand, returns true else false`(a >= b)` returns true
`<=`If value of right operand is greater or equal to left operand, returns true else false`(a <= b)` will return false
Logical operators
Logical operators are used to combine two boolean expression together and results a single boolean value according to the operand and operator used.
`&&`Used to combine two expressions. If both operands are true or Non-Zero, returns true else false`((a>=1) && (a<=10))` returns true since `(a>=1)` is true and also `(a<=10)` is true.
`||`If any of the operand is true or Non-zero, returns true else false`((a>1) || (a<5))` will return true. As `(a>1)` is true. Since first operand is true hence there is no need to check for second operand.
`!`Logical NOT operator is a unary operator. Returns the complement of the boolean value.`!(a>1)` will return false. Since `(a>1)` is true hence its complement is false.
Bitwise operators
Bitwise operator performs operations on bit(Binary level). Lets suppose `a = 10, b = 5`
`a = 0000 1010 (8-bit binary representation of 10)`
`b = 0000 0101 (8-bit binary representation of 5)`
`&` Bitwise AND performs anding operation on two binary bits value. If both are 1 then will result is 1 otherwise 0.
``````  0000 1010
& 0000 0101
____________
0000 0000``````
`|` Bitwise OR returns 1 if any of the two binary bits are 1 otherwise 0.
``````  0000 1010
| 0000 0101
___________
0000 1111``````
`^` Bitwise XOR returns 1 if both the binary bits are different else returns 0.
``````  0000 1010
^ 0000 0101
___________
0000 1111``````
`~` Bitwise COMPLEMENT is a unary operator.It returns the complement of the binary value i.e. if the binary bit is 0 returns 1 else returns 0.
``````~ 0000 1010
___________
1111 0101``````
`<<` Bitwise LEFT SHIFT operator is unary operator. It shift the binary bits to the left. It inserts a 0 bit value to the extreme right of the binary value.
``````  0000 1010 << 2
= 0010 1000``````
`>>` Bitwise RIGHT SHIFT operator is unary operator. It shifts the binary bits to the right. It inserts a 0 bit value to the extreme left of the binary value.
``````  0000 1010 << 2
= 0000 0010``````
Increment/Decrement operator
Increment/Decrement operator is a unary operator used to increase an integer value by 1 or decrease it by 1. Increment/decrement operator are of two types Postfix and Prefix.
`++`Increment operator will add 1 to an integer value.`a++` will result to 11
`++a` will result to 11
`--`Decrement operator will subtract 1 from an integer value.`a--` will result to 9
`--a` will result to 9
Conditional/Ternary operator
Ternary operator as a conditional operator and is similar to simple if-else. It takes three operand.
`?:`It is used as conditional operator. Syntax of using ternary operator:
`(condition) ? (true part) : (false part)`
`b = (a>1) ? a : b;`
will store the value 10 in b as (a>1) is true hence true part will execute, assigning the value of a in b.

### Other operators

In addition to above mentioned operator, C supports many more operators.

OperatorNameDescription
`.`Member access operatorUsed to access the members of structures and unions
`->`Member access operatorUsed to access the members of structures and unions
`*`Dereferencing operatorUsed to dereference the value of a pointer variable
`&`Address of operatorUsed to get the actual memory address of a variable
`sizeof()`Size of operatorUsed to get the size of a data type

At this point discussing these operators is not possible. I will introduce these operators later in this C programming tutorial series.

Recent Posts
Popular Posts