Operators in C Language explained with 3 Examples
a) Arithmetic
b) Relational
c) Logical
d) Bitwise
e) Increment & Decrement operators
f) Assignment operators
a) Arithmetic operation:-
These operators to calculate mathematical operations.these are-, +,*,%,÷.Subtraction(-)
- it is used to subtract two operands
- A-B=1
- A=3,B=2
Addition (+)
- It is useful to add two operands
- A+B=5 (refer example 3)
Multiplication (*)
- It is used to multiplies two operandsA*B=6
Division (/)
- It is used to divides numerator by de-numerator
- A/B=1 (refer example 1&2)
%Remainder
- It is useful to get remainder
- A%B=0
Example 1)Division:-
int a, b;
float c;
c=a/b
Result :
a=10
b=3
3.000
To get 3.333 in result
We have to small change see below program
We have to small change see below program
Example 2)Division
Int a, b;Float c;
c=a/b*1.0
Example 3)Addition:-
int a, b;float c;
C=a+b
b ) Relational operators
These operators are used to compare two Numbers, ==,>,>,<if the condition is true them the result will be 1 otherwise result will be 0
Example 1)
int a, b, c;a=10;
b=3;
c=a==b;
Now c is zero condition is false
Example 2)
int a,b,c;a=100;
b=55;
c=a> b;
now c is one because condition is true;
C) Logical operators
In these operation every operator perform different operation&&
||
!
Truth table for && operator
T T T
T F F
F T F
F F FTruth table for | operator
T T TT F T
F T T
Example 1
int a,b,c;a=100;
b=400;
c=a==b&&b> 300;
Now c=0; because condition is false
Note: In these example1 we are using && operator
Example 2
int a,b,c;a=100
b=400
c=a== b|| b> 300;
Now c=1, observe conditions b> 300 satisfied so result is true
D) Bitwise operators
These operators are performed on bitsExample 1)
a=25;b=13;
c=a/b;
Result is 29
Explanation
25→11001
13→01001
11101
Comments
Post a Comment