Operators: The operator is a symbol that is used to perform a specific task. the operator tells the compiler to perform arithmetic or logical operation.
The operator mainly divided into two categories the binary operator and the unary operator.
Binary operator: An operator that required a minimum of two operands to perform a certain task is known as a binary operator.
Operand1 operator operand2
Unary operator: An operator that required only a single operand to perform a certain task is known as a unary operator.
Types of Operators
- Arithmetic operator
- Assignment operator
- Relational operator
- Logical operator
- Increment and decrement operator
- Conditional or ternary operator
- Bitwise operator
- Special operator
Arithmetic operator: The operator that is used to perform algebraic operations between two or more operands. it is a binary operator.
There are following operators are used in arithmetic operator
Divisions of integer numbers truncate floating-point value. % operator returns the remainder of the operand and it does not use in floating data type value.
int a=10, b=20, c=3;
a+b result 30
b%c result 2
c/3 result 1
a*b result 200
a-b result -7
Assignment operator: This operator is used to assigning a value to a particular operand. this is a binary operator, It is also known as a shorthand operator. operation is performed first, Operation such as a +, -, *, /, %, and then it assigns the value.
Following operators are used in assignment operators
Relational operator: This operator define the relation between two operands. this operator is used to compare one operand to another operand. It is also used to compare arithmetic or logical expression. It returns either true or false or 1 or 0.
following are relational operators.
int x=10, y=20;
x<y; result true or 1
x>y; result false or 0
Logical operator: Logical operators are used when to test more than one condition for expression. it always returns 1 or 0.
There are following operators are used in logical operator.
&&(and): It will return 1 when all expressions are returned 1 otherwise will return 0.
int a=10, b=20;
a>=b && a<=20 result is 0
If first expression return 0 then it cannot evaluate another expression.
If first expression return 1 then it will evaluate another expression.
||(or): It will return 1 when either any expression return 1 otherwise 0.
int a=10, b=20;
a>=b || a<=20 result is 1
If 1 found in first expression then it doesn’t evaluate another expression.
!(not): This operator returns the opposite value of a particular expression. if expression return 1 then result is 0 if expression return 0 then result is 1.
!(a>20) result is 1
!( a<20) result is 0
Increment/decrement operator: ++, – – is known as increment or decrement operators.
This operator is used when we want to increase or decrease the value by 1. it is also known as a prefix for a postfix operator.
Prefix: ++X first add 1 to operand and result be assigned to the variable on left operand is known as a prefix operator.
Y=++X; output X=11, Y=11;
Postfix: X++ first assign the value to the variable on the left hand and then increment the operand is known as a postfix operator.
Y=X++; output X=10, Y=11;
Conditional or ternary operator: this operator is used to check condition select value depending on the value of the condition. it is also known as a ternary operator because it requires a minimum of 3 operands or expressions to perform an operation.
int a, b=10, c-20;
If expression 1 return true then expression 2 will be evaluated otherwise expression 3 will be evaluated this is the same as an if-else statement.
Bitwise operator: This operator is used to check bit by bit value. This operator is used when our program required to test value at a bit level.
This operators are non applicable for float and double data type.
Following operators are used in bitwise operator.
& bitwise and
| bitwise or
^ exclusive or
~ one’s complement
<< left shift
>> right shift
Special operator: Following operators are known as a special operator.
- , (comma)
1.sizeof: This operator finds out the number of bytes occupied by the variable for a data type.
printf(“%d”,sizeof(a)); output 2 bytes
2.comma(,): This operator is used to define more than 1 variable in a single statement.
int a, b, c;
It is also use to link to multiple expressions.