Operator in C
Operator in C
In C Programming language, operator are symbols that represent Operation to be perform on one or more operands. An operators specified an operation to be performed that yields a value.
Here are some commonly used operator in C :
1. Arithmetic Operators
Arithmetic operators are used for numeric calculations. These Operators are used to perform for mathematical/arithmetic operations on operands. They are of two types-
unary operators require only one operand. for example:
+x -y
here '-' changes the sign of the operand y.
b. Binary Arithmetic operators
Binary operators require two operands. There are five arithmetic operators-
Operator | Purpose |
+ | addition |
- | subtraction |
* | multiplication |
% | Gives the remainder in integer division |
NOTE : %(modulus operator) cannot be applied with floating point operands. There is no exponent operator in C, however there is a library function pow() to carry out exponentiation operation.
Program to demonstrate arithmetic operator:
Sum = 21
Subtraction = 13
Multiplication = 68
Division = 4
Remainder=1
2. Assignment Operators
Assignment operator are used to assign the value to variable. The operand on the left hand side should be a variable, while the operand on the right hand side can be any variable, constant or expression. some assignment operators are (=, +=, -=, *=, /=)
x = 8 /* 8 is assigned to x*/
a=16
b=9
3. Increment and Decrement Operators
The increment(++) and decrement(--) operators are unary operators because they operate on a single operand. The increment operator increments the value by 1, while decrement operator decrements the value by 1.
++x is equivalent x=x+1
--x is equivalent x=x-1
Note : ++5 or ++(x+y) are Invalid
These operators are of two types -
2. postfix increment/decrement-operator is written after the operand(e.g. x++,x--)
program to demonstrate prefix increment and decrement operators:
a=9 a=10 a=10 a=9 a=9
program to demonstrate postfix increment and decrement operators:
a=9 a=9 a=10 a=10 a=9
4. Relational Operators
Relational operators are used to compare values of two expression. If the relation is true then the value of relational expression is 1 and if the relation is false then the value of expression is 0. The relational operators are -
operator | Meaning |
---|---|
< | less than |
<= | less than equal to |
== | equal to |
!= | not equal to |
> | greater than |
>= | greater than or equal to |
program to demonstrate relational operator :
Enter the value of a and b:
6
8
6 is less than 8
6 is less than or equal to 8
6 is not equal to 8
operator | Name |
---|---|
&& | AND |
|| | OR |
! | NOT |
condition 1 | condition 2 | Result |
---|---|---|
False | False | False |
False | True | False |
True | False | False |
True | True | True |
condition 1 | condition 2 | Result |
---|---|---|
False | False | False |
False | True | True |
True | False | True |
True | True | True |
condition | Result | |
---|---|---|
False | True | |
True | False |
Program to demonstrate Logical operators
Enter the value of a and b:
0
2
a && b = 0
a || b = 1
!a = 1
6. Conditional Operators
Conditional operator are also called Ternary operator (? and :) which required three expression as operands. It is written as -
If test expression is true(non-zero), then expression1 is evaluated otherwise test expression is false(zero), then expression2 is evaluated.
We may replace the use of if..else statements with conditional operators.
Program to demonstrate Logical operators
Enter the value of a and b:
25
23
larger of 25 and 23 is 25
7. Bitwise Operators
C has the ability to support manipulation of data at the bit level. Bitwise operators operate on integers only and they are used for operations on individual bits. The bitwise operators are:
Bitwise operator | Meaning |
---|---|
& | bitwise AND |
| | bitwise OR |
~ | One's complement |
<< | left shift |
>> | right shift |
^ | bitwise XOR |
The value of a =5 and b=9
The value of a&b is 1
The value of a|b is 13
The value of a^b is 12
The value of a>>1 is 2
The value of a<<1 is 10
The value of ~a is -6
Thank you
Comments
Post a Comment