Brainware University: Pre/post Increment Arithmetic Operator Relational Operator
Brainware University: Pre/post Increment Arithmetic Operator Relational Operator
⚫ b //relational expression
⚫ a= =b // logical expression
Operator:
This is a symbol use to perform some operation on variables, operands or with the constant.
Some operator required 2 operand to perform operation, Some required single operand to
perform operation and some require 3 operand to perform operation.
Depending upon the numbers of operands required to perform operations, operands are divided
in 3 types
Operator
Unary Operator: unary operator takes only one operand to perform its operation. Example are
pre increment(++i), post increment(i++), pre decrement(--i),post decrement(i++), unary +/-,
logical NOT operator, pointer operator.
Pre Increment/ Decrement:- In the prefix increment/decrement the value of the variable is
incremented/decremented 1st , then the new value is used.
EXAMPLE
void main() {
int y=12, z;
z= ++y;
printf(“y =%d, z=%d”,y,z);
}
Output:- y=13,z=13
In this example value of y is incremented 1st then the incremented value is assigned to z. That’s
why value of both variables are 13.
Post Increment/Decrement:- In the postfix increment and decrement operator is used in the
operation . And then increment and decrement is perform.
EXAMPLE
void main() {
int y=12, z;
z= y++;
printf(“y =%d, z=%d”,y,z);
}
Output:- y=13,z=12
In this example value of y is assigned to z 1st then the value of y is incremented.That’s why
value y=13 and z=12.
i++ ++i
1. i++ is called post increment operator 1. ++i is called pre increment operator
y=++i; y=i++;
prntf(“y=%d”,y) prntf(“y=%d”,y)
} }
3. i++ first assign the value to the vaiable n 3. ++i first adds 1 to the operand and then
left and then increments the operand. the result is assigned to the variable on the
left.
Binary Operator
Arithmetic Operator:
This operator used for numeric calculation. Arithmetic operators are addition(+), subtraction(-),
multiplication(*), division(/) and modulus(%). But modulus cannot applied with floating point
operand as well as there are no exponent operator in c.
When both the operand are integer then it is called integer arithmetic and the result is always
integer. When both the operand are floating point then it is called floating arithmetic and when
operand is of integer and floating point then it is called mix type or mixed mode arithmetic . And
the result is in float type.
void main()
{
int a=8,b=3,c;
c=a+b;
printf(“Result of addition=%d”,c);
c=a-b;
printf(“Result of subtraction=%d”,c);
c=a*b;
printf(“Result of multiplication=%d”,c);
c=a/b; // fractional part of the result is removed as both of the operands are integer
printf(“Result of division=%d”,c);
c=a%b; // returns the remainder of the division
printf(“Remainder is=%d”,c);
}
Relational Operator
It is use to compared value of two expressions depending on their relation. Expression that
contain relational operator is called relational expression. Here the value is assign according to
true or false value. The followings are relational operator.
== equal to equal
!= not equal to
void main(){
int a= 6, b = 4, c;
c = a >= b;
printf(“The comparison returns %d ”, c);
c = a < =b;
printf(“The comparison returns %d ”, c);
c = a == b;
printf(“The comparison returns %d ”, c);
c = a != b;
printf(“The comparison returns %d ”, c);
}
Output: The comparison returns 1
The comparison returns 0
The comparison returns 0
The comparison returns 0
Operator used with one or more operand and return either value zero (for false) or one (for true).
The expression that combines two or more expressions is termed as logical expression. C has
three logical operators
4
|| Logical OR
! Logical NOT
Among which Logical NOT operator is an unary operator. Remaining are binary operator..
Logical AND gives result true if both the expression are true, otherwise result is false. And
logical OR gives result false if both the expression false, otherwise result is true.
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
Bit-wise Operator:-
Bit-wise operator permit programmer to access and manipulate of data at bit level. Various bit-
wise operator enlisted are
bitwise OR (|)
These operator can operate on integer and character value but not on float and double.
Example
void main() {
int = 50, b=5, r;
r = a & b;
printf(“Bitwise AND operator- result is %d”,r);
r = a | b;
printf(“Bitwise OR operator- result is %d”,r);
r = a ^ b;
printf(“Bitwise XOR operator- result is %d”,r);
r = a << 2;
printf(“Bitwise left shift operator- result is %d”,r);
r = a >> 2;
printf(“Bitwise right shift operator- result is %d”,r);
}
Output:
Bitwise AND operator- result is 0
Bitwise AND operator- result is 55
Bitwise XOR operator- result is 55
Bitwise left shift operator- result is 200
Bitwise right shift operator- result is 12
Assignment Operator:-
A value can be stored in a variable with the use of assignment operator. The assignment
operator(=) is used in assignment statement and assignment expression. Operand on the left hand
side should be variable and the operand on the right hand side should be variable or constant or
any expression.
Example
When variable on the left hand side is occur on the right hand side then we can avoid by writing
the compound statement.
A=A*B
=>A*=B
Precedence of operators: - If more than one operators are involved in an expression then, C
language has predefined rule of priority of operators. This rule of priority of operators is called
operator precedence. The operator at the higher level of precedence are evaluated first.
Associativity of operator: - Associativity is only used when there are two or more operators of
same precedence. Associativity doesn’t define the order in which operands of a single operator
are evaluated. Associativity is either left to right (L->R) or right to left (R->L).
1 Left to right
() function call
[] array subscript
--> arrow operator
. dot operator
2 Right to left
+ unary plus
- unary minus
++ increment
-- decrement
! logical not
~ 1’s complement
* indirection
& address
sizeof size in byte
3 Left to right
% modulus
* multiplication
/ division
Subtraction -
!= not equal to
|| logical OR 12
, comma operator 15
Ternary Operator
It sometimes called as conditional operator. Since it required three expressions as
SYNTAX
Here exp1 is first evaluated. It is true then value return will be exp2 . If false then exp3.
EXAMPLE
void main()
printf(“value is:%d”);
Output:
Value is:10