Operators and expressions
C language supports a set of built in operators such as =,
+, -, *, /, <, >, etc all are symbol that tell the computer to perform
task or any mathematical or logical operation. Operators are use to manipulate
the data and variables. There are some types of operators like,
1.
Arithmetic operators
2.
Relational operators
3.
Logical operators
4.
Assignment operators
5.
Increment and decrement operators
6.
Conditional operators
7.
Bitwise operators
8.
Special operators
Arithmetic operator:- arithmetic operator means the arithmetic
operation done through those operator. The basic arithmetic operators are +, -,
/, *
Operator
|
Meaning
|
+
|
Addition
|
-
|
Subtraction
|
*
|
Multiplication
|
/
|
Division
|
%
|
Modulo division
|
Integer operation:
Addition : the addition operator
are use to add the two operand.
A = 5 + 10 means add 5 and 10
and store the value 15 in A.
Subtraction: the subtract
operator is used to subtract the value from left operand to the right operand .
A = 5 – 10 means subtract 10
from 5 and store the value in A.
Multiplication: the
multiplication operator is used to multiply the operand to each other.
A = 5 * 10 means multiply the
operand to each other and store the value in A.
If the expression is A = 10 *
3.5 the result will not get as our expected, we will expect the result is 17.5
but the result we get that is 17 because the variable is an integer type and
can’t store the fractional part so we get the result like that.
Division: division operator is
used to divide the right operand from the left operand and store the value in
A.
Real/floating-point
operation: The real operation
means the arithmetic operation between the real operand and we can do this type
of operation through float data type, float is real data type where we can work
with the fractional numbers.
Example:
Addition : the addition operator
are use to add the two operand.
A = 5 + 10.5 means add 5 and
10.5 and store the value 15.5 in A. As the operand/variable is float type.
Subtraction: the subtract
operator is used to subtract the value from left operand to the right operand .
A = 5.5 – 10 means subtract 10
from 5.5 and store the value -4.5 in A, as the operand is a real type variable
or float type that’s why we can store the exponential value in A.
Multiplication: the multiplication
operator is used to multiply the operand to each other.
A = 5 * 10.5 means multiply the
operand to each other and store the value 52 in A.
Division: division operator is
used to divide the right operand from the left operand and store the value in
A.
A = 10 /3 the result will be 3.333333
as expected, here we get the perfect result because the variable is real type
that why the variable can hold the floating point result.
2. Relational operators: relational operator means the operator which
compares two operands depends on their relation and the value, for example if
we compare two marks then this type of comparison can be done with the
relational operator, like >, <.
C support the different types of relational operators.
Operator
|
Meaning/job
|
<
|
Less than
|
>
|
Greater than
|
<=
|
Less than equal to
|
>=
|
Greater than equal to
|
==
|
Equal
|
!=
|
Not equal
|
Example of some relational
operation:
Less than (<) : 5<10 means
is 5 less than 10, 5 < 10 expression is true.
Greater than(>) : 5 > 10
means is 5 greater than 10 expression is false
Less equal to: 10<= 9 means
is 10 less or equal to 9 expression is true
10 <= 10 means is 10 is less
or equal to 10 expression is true
10 <= 11 means is 10 less or
equal to 11 expression is false because does not fulfil any one criteria.
Not equal : not equal means
complement of the operand, if any expression is ( A! = 5 ) means if the value of ‘A’ is 5 then the result will
be false other wise any value of the operand ‘A’ is true except 5 .
Logical operator: when we want to compare the two or more
relation with the logical operators.
C has the following logical
operators.
&& is logical AND
|| is logical OR
! is logical NOT
Operator 1
|
Operator 2
|
AND operation
|
OR operation
|
Non zero
|
Non zero
|
1
|
1
|
Non zero
|
0
|
0
|
1
|
0
|
Non zero
|
0
|
1
|
0
|
0
|
0
|
0
|
Logical AND : when the both expression are true then logical
AND returns true value and if one of them are false then the logical and return
false or zero(0), if both two expression are false then also the logical AND
returns false or zero.
Example:
(10 > 5) && (8>10)
is false because the left expression which is 10 > 5 is true but the left
expression which is 8 > 10 false but the logical OR operator return false if
any of the expression is false.
Expression
1
|
Expression
2
|
Exp
1 && Exp 2
|
Exp
1 || Exp 2
|
True
|
True
|
True
|
True
|
True
|
False
|
False
|
True
|
False
|
True
|
False
|
True
|
False
|
False
|
False
|
false
|
Logical OR : when one of the two expression is true then
logical OR returns true if both of expression is false then the logical or
returns false or zero(0).
Example:
(10 > 5) ||(8>10) is true
because the left expression which is 10 > 5 is true but the left expression
which is 8 > 10 false but the logical OR operator return true if any of the
expression is true.
Logical Not : Logical not means the complement if the
expression is true then the logical not returns false if false then returns
true.
Assignment operator : Assignment operator is used to assign
a value or the result of an expression to a variable.
Example:
if A = 10 that means put the value 10 into ‘A’
which is assign a value in ‘A’.
If A = 10 * 5 that means we have
to complete the operation that means 10 * 5 result is 50 and then put the value
50 in ‘A’
There are some concept of
shorthand operator means A += 10 * 5 that means first multiply 10 and 5 and
then add with value of variable ‘A’ and store the final value in ‘A’. A +=10 *
5 is same as A = A + (10 * 5).
Increment and decrement operator: standard C language
support the increment and decrement operator which are used to increment and
decrement. The increment operator is ++ and decrement operator is – , and the
operators are unary operator.
Example: A++ means
increment one the value of ‘A’ and store the new value in ‘A’. If A = 10 and
apply A++ that means increment one the value of ‘A’ and store the value 11 in
‘A’A++ is equal to a = a + 1 and ++A is also is same.
A-- means decrement one the
value of ‘A’ and store the new value in ‘A’. If A = 10 and apply A-- that means
decrement one the value of ‘A’ and store the value 9 in ‘A’, A-- is equal to a
= a - 1 and A-- is also is same.
The increment and decrement
operator are two types pre increment and pre decrement and post increment and
post decrement.
|
Pre operation : pre operation
two types one is pre increment and other one is predecrement operation.
If A = 10, A ++ and ++ A are
same both increment one but there is some differences when we apply them in a programme.
Example:
A = 10; A
= 10
Printf(“%d”, ++A); printf(“%d”,A++);
Output : 11 Output:
10
The pre increment or pre
decrement and post increment or post decrement concept is that both operations
are same thing but if there is an expression then the pre operation is
Conditional operator: In C the conditional operator is “? :”
also called ternary operator, when we construct any conditional expression.
Example : A = 10
B = 5
X
=( A > B ) ? A : B;
Here we use three variable A
=10, B = 5, and the result of the expression is the value of X. The meaning of
the expression is ‘A’ is greater than B
then, if the condition is true then return the true part that is ‘A’ if the
condition is false then return the false part that is ‘B’.
(expression 1 CONDITION expression 2 ) ? true statement : false
statement
Here expression 1 is ‘A’ and expression 2 is ‘B’ and the
condition is greater than operator ( >) and true statement is ‘A’ and false
statement is ‘B’.
Bitwise operator :
standard C language support special type of operators that can manipulate the
bit level is call bitwise operator, bitwise operator is very use full for bit
shifting operation.
The bitwise operators are :
Operator
|
Meaning/job
|
&
|
Bitewise AND
|
|
|
Bitwise OR
|
^
|
Bitwise exclusive OR
|
<<
|
Left shift
|
>>
|
Right shift
|