Operators are a symbol that operates on a value or variable,
for example, let’s have a look at this expression 3 + 2 is equal to 5, here + is an operator that operates on 3 and 2, So + is an operator to perform addition
and 3, 2 are called as operands.C Language supports the following type of operators.
- Arithmetic operators
- Logical (&) Relational operators
- Bitwise operators
- Assignment operators
- Special operators
- Conditional operator
C Programming Language supports following arithmetic operators
|+||Performs addition or unary plus|
|–||Subtraction or unary minus|
|/||Division (quotient after division)|
|%||Modulo Division (Remainder After Division)|
|++||increments operands by 1 (Increment operator)|
|—||Decrements operands by 1 (Decrement Operator)|
Let’s see a sample program
#include int main()
c=a+b; /******** + operator******** adds a and b /
c=a-b; /******** - operator ******** subtracts a and b/
c=a*b; /////////////// * operator ////////////// multiplies a and b/
c=a/b; /******* / operator******** Divides a and b/
c=a%b; /******* % operator ****** gives remainder after division/
printf("Remainder when a divided by b=%d\n",c);
a++; /******** ++ operator ****** increments a by 1/
printf("after increment a value=%d\n",a);
b--; /******** -- operator ******** Decrements by 1/
printf("after increment b value=%d\n",b);
Remainder when a divided by b=3
after increment a value = 4
after decrement b value = 3
there is a usage difference between increment (++) operator and (–) Decrement operator and these operators can be used as prefix or postfix, like below
let a is variable
When ++ is used as a refix, ++a will increment the value of a by 1 and then return it, if ++ is used as postfix(a++), operator will return the value of operand first and then increment it.
Logical (&) Relational operators:
Relational operators always checks relationship between two operands, it always gives binary result like 1 or 0 always,
example : a > b , where > is relational operator , if a is greater than b , it gives 1 else 0.
Logical operators are used to combine expressions containing relation operators.
example : (a > b) && (a > c),where && is Logical operator.
C supports following Relational and logical operators
|>||greater than||1 > 2 return false|
|<||Less than||1 < 2 return true|
|>=||Greater than or equal to||1>= 2 return false|
|<=||Less than or equal to||1 <= 2 return false|
|==||equal to||2 == 2 return true|
|!=||not equal to||1 != 2 return true|
|&&||Logical And|| if a=2 b = 1 (a > b) &&(a< 10) return true
both expression a > b and a < 10 should be true,
|||||Logical Or||if a=2 b = 1 (a > b) || (a> 10) return true.
either expression should be true,Since a>b is true
above expression gives result true.
|!||Logical not||if a =10 , !(a==10) return false.|
Conditional operator consists two symbol ? and : and it takes three operands
example: Let a = 10;
c = (a>5) ? 15 : 5
if a is greater than 5, it results 15 else 5, in our example a = 10, c = 15.
Bitwise operator works in bit level programming, it works on each bit.
Comma operators are used to link related expressions together, example int a, b;
The size of Operator:
It is a unary operator which is used finding the size of a data type, constant, arrays, structure etc.
sizeof(int) gives size of an integer data type as 4 bytes.
Assignment operators have two values L value and R value, it is used to assign the value to variable,most common assignment operator is =
a = 10 10 is assigned to a
a = b value of b assigned to a