Operators in C
An operator is a special token that performs an operation, such as addition or subtraction, on either one, two, or three operands.
C language is rich in built-in operators and provides the following types of operators
1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Bitwise Operators
5) Assignment Operators
6) Ternary or Conditional Operator
Given table shows all the Arithmetic operator supported by C Language. Lets suppose variable A hold 10 and B hold 20.
|+ ||Adds two operands. ||A + B = 30 |
|- ||Subtracts second operand from the first. ||A - B = -10 |
|* ||Multiplies both operands. ||A * B = 200 |
|/ ||Divides numerator by de-numerator. ||B / A = 2 |
|% ||Modulus Operator and remainder of after an integer division. ||B % A = 0 |
Which can be used to check the Condition, it always return true or false. Lets suppose variable A hold 8 and B hold 3.
|Operators ||Example (int A=8, B=3) ||Result |
|< ||A < B ||False |
|<= ||A<=10 ||True |
|> ||A>B ||True |
|>= ||A<=B ||False |
|== ||A== B ||False |
|!= ||A!=(-4) ||True |
Which can be used to combine more than one Conditions. Suppose you want to combined two conditions A<B and B>C, then you need to use Logical Operator like (A<B) && (B>C). Here && is Logical Operator.
|Operator ||Example (int A=8, B=3, C=-10) ||Ouput |
|&& ||(A < B) && (B> C) ||False |
||| ||(B!=-C) || (A==B) ||True |
|! ||!(B<=-A) ||True |
Which can be used to assign a value to a variable. Lets suppose variable A hold 8 and B hold 3.
|Operator ||Example (int A=8, B=3) ||Ouput |
|+= ||A+=B or A=A+B ||11 |
|-= ||A-=3 or A=A+3 || 5 |
|*= ||A*=7 or A=A*7 ||56 |
|/= ||A/=B or A=A/B ||2 |
|%= ||A%=5 or A=A%5 ||3 |
|=a=b ||Value of b will be assigned to a || |
Operator which operates on single operand is called Unary Operator.Assume variable A holds 10 and variable B holds 20 then
|Operator ||Description ||Example |
|++ ||Increment operator increases the integer value by one. ||A++ = 11 |
|-- ||Decrement operator decreases the integer value by one. ||A-- = 9 |
What next ?
Introduction to Lexical Elements in C