Pages

Thursday 19 March 2020

Programs to illustrate operators

Operators:
We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands.

For example :
consider the statement c = a + b;
Here, ‘+’ is the operator known as addition operator and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.

Operators are classified as follows:
  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Increment  Operators
  6. Decrement Operators
  7. Assignment Operators
  8. Ternary or Conditional Operators
  9. Special Operators

1.Arithmetic Operators ((5) : + ,  - ,  *  , %  , /):
These are the operators used to perform arithmetic/mathematical operations on operands. 
Examples: (+, -, *, /, %,++,–). 
Arithmetic operator are of two types:
Unary Operators: Operators that operates or works with a single operand are unary operators. For example: (++ , –)
Binary Operators: Operators that operates or works with two operands are binary operators. For example: (+ , – , * , /)

/* Program to illustrate 
Arithmetic Operators { (5) : +, -, *, %, / } */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
#define sf scanf
void main()
{
int a,b,r;
clrscr();
pf("\n enter a value :");
sf("%d",&a);
pf("\n enter b value :");
sf("%d",&b);
r=a+b;
pf("\n Result is %d",r);
getch();
}

2.Relational Operators((6) : < , <= , > ,  >= , == , !=): 
These are used for comparison of the values of two operands.
For example, checking if one operand is equal to the other operand or not, an operand is greater than the other operand or not etc. Some of the relational operators are (==, !=, >= , <= , <, >)

/* Program to illustrate 
 Relational Operators { (6) : <,>,<=,>=,==,!= } */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
#define sf scanf
void main()
{
int a,b,r;
clrscr();
pf("\n enter a value :");
sf("%d",&a);
pf("\n enter b value :");
sf("%d",&b);
r=(a<b);
pf("\n Result is %d",r);
getch();
}

NOTE: Relational operators can be used to combine more than one arithmetic expressions
for example : (a+b) >(c*d)

/* Program to illustrate relational operators can be used to combine more than one   arithmetic expressions */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
#define sf scanf
void main()
{
int a,b,c,d,r;
clrscr();
pf("\n enter a value :");
sf("%d",&a);
pf("\n enter b value :");
sf("%d",&b);
pf("\n enter c value :");
sf("%d",&c);
pf("\n enter d value :");
sf("%d",&d);
r= (a+b)<(c*d);
pf("\n Result is %d",r);
getch();
}
 
OUTPUT:

3.Logical Operators ((3): && , || , !):  
Logical Operators are used to combine two or more conditions/constraints. The result of the operation of a logical operator is a Boolean value either true or false.

For example, the logical AND represented as ‘&&’ operator in C returns true when both the conditions under consideration are satisfied. Otherwise it returns false. 
Therefore, a && b returns true when both a and b are true (i.e. non-zero)

a

b

a &&b

0

0

0

0

1

0

1

0

0

1

1

1


For example, the logical OR represented as ‘||’ operator in C returns false when both the conditions under consideration are not satisfied. Otherwise it returns true. 
Therefore, a || b returns false when both a and b are false (i.e. zero)

a

b

a ||b

0

0

0

0

1

1

1

0

1

1

1

1

For example, the logical NOT represented as ‘|’ operator in C returns true for the given false condition similarly it gives false for the given true condition  
Therefore, if a is  true then the logical output  of a is false, similarly if a is false then the logical output is true

a

!a

0

1

1

0

/* Program to illustrate 
Logical Operators { (3) :  &&,   ||  ,  |   } */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
#define sf scanf
void main()
{
int a,b,r;
clrscr();
pf("\n enter a value :");
sf("%d",&a);
pf("\n enter b value :");
sf("%d",&b);
r=(a&&b);
pf("\n Result is %d",r);
getch();
}

NOTE: Logical operators can be used to combine more than one relational expressions
for example : (a>b)&&(c<d)

/* Program to illustrate logical operators can be used to combine more than one relational expressions */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
#define sf scanf
void main()
{
int a,b,c,d,r;
clrscr();
pf("\n enter a value :");
sf("%d",&a);
pf("\n enter b value :");
sf("%d",&b);
pf("\n enter c value :");
sf("%d",&c);
pf("\n enter d value :");
sf("%d",&d);
r=(a>b)&&(c<d);
pf("\n Result is %d",r);
getch();
}

OUTPUT:

4.Bitwise Operators ((6) : & , | , ^ , ~ , << , >>): 
The Bitwise operators is used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. The mathematical operations such as addition, subtraction, multiplication etc. can be performed at bit-level for faster processing. 

The following are the 6 Bitwise operators 
1.Bitwise AND (&) represented as & operator in C takes two numbers as operands and does AND gate function on every bit of two numbers. The result of AND is 1 only if both bits are 1.
For example:
  a  = 14 --------------1110     
  b  = 9    -------------1001
(Result a&b : 8) ---1000

2.Bitwise inclusive OR (|) represented as | operator in C takes two numbers as operands and does OR gate function on every bit of two numbers. The result of OR is 1 only if any one of the bits are 1.
For example:
  a  = 14  -------------1110     
  b  = 9    -------------1001
(Result a|b: 15)  ---1111

3.Bitwise exclusive OR ( ^) represented as ^ operator in C takes two numbers as operands and does x-OR gate function on every bit of two numbers. The result of OR is 1 only if any one of the bits are 1.
For example:
  a  = 14  -------------1110     
  b  = 9    -------------1001
(Result a^b: 7)  ---0111

4.Bitwise NOT(~) represented as ~ operator in C takes one number as operands and does NOT gate function  on every bit.
For example:
  a  = 14  -------------1110     
(Result ~a: -15)  ---1's complement is  0001 
To this result perform 2's complement i.e negative number 
  1110
+       1
---------
  1111 which is -15

For example:
  a  = 9  -------------1001     
(Result ~a: -10)  ---1's complement is  0110 
To this result perform 2's complement i.e negative number 
  1001
+       1
---------
   1010 which is -10
/*Program to illustrate 
Bitwise not( ~) and logical not (!) */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
#define sf scanf
void main()
{
int a,b,bn,ln;
clrscr();
pf("\n enter a value :");
sf("%d",&a);
pf("\n enter b value :");
sf("%d",&b);
ln=(!a);
pf("\n Logical not '!': %d",ln);
bn=(~b);
pf("\n Bitwise not '~': %d",bn);
getch();
}
OUTPUT:

5.Bitwise right shift >> represented as >> operator in C takes one number as operands and does right shift one bit function  on every bit.
For example:
  a  = 9      -------------1001     
(Result a>>2: 2)  ---  0010

6.Bitwise left shift << represented as << operator in C takes one number as operands and does left shift one bit function  on every bit.
For example:
  a  = 9   --------------------1001     
(Result a<<2: 36)  --- 100100

/* Program to illustrate 
Bitwise Operators { (6) : >>, <<, &, |, ~ } */

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
#define sf scanf
void main()
{
int a,b,r;
clrscr();
pf("\n enter a value :");
sf("%d",&a);
pf("\n enter b value :");
sf("%d",&b);
r=(a&b);
pf("\n Result AND '&': %d",r);
r=(a|b);
pf("\n Result inclusive OR '|': %d",r);
r=(a^b);
pf("\n Result exclusive  '^': %d",r);
r=(~a);
pf("\n Result NOT '~': %d",r);
r=(a>>2);
pf("\n Result right shift '>>': %d",r);
r=(a<<2);
pf("\n Result left shift '>>': %d",r);
getch();
}

output:


5,6  Increment,
Decrement Operators (++ , -- ):
Both are useful operators generally used to minimize the calculation,
Operator   Description
++             Increment
−−             Decrement
i.e. ++x and x++ means x=x+1 or --x and x−− means x=x-1.
But there is a slight difference between ++ or −− written before or after the operand. Applying the pre-increment first add one to the operand and then the result is assigned to the variable on the left  whereas post-increment first assigns the value to the variable on the left and then increment the operand.
Example: 


/* Program to illustrate Increment and Decrement Operators */ 
#include <stdio.h>
void main()
{
    //set a and b both equal to 5.
    int a=5, b=5;
    //Print them and decrementing each time.
    //Use postfix mode for a and prefix mode for b.
    printf("\n%d %d",a--,--b);
    printf("\n%d %d",a--,--b);
    printf("\n%d %d",a--,--b);
    printf("\n%d %d",a--,--b);
    printf("\n%d %d",a--,--b);
}

Output:
5 4
4 3
3 2
2 1
1 0

7.Assignment Operators ((6) : =,+=,-=,*=,/=,%= ): 
Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of variable on the left side otherwise the compiler will raise an error.

The following are the 
assignment operators {(6) : =,+=,-=,*=,/=,%= }

“=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.
For example:
a = 10;
b = 20;
ch = 'y';
 
“+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on right and then assigns the result to the variable on the left.
Example:
(a += b) can be written as (a = a + b)
If initially value stored in a is 5. 
Then (a += 6) 
        (a = a + 6) 
        (a = 11).

“-=”: This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the value on right from the current value of the variable on left and then assigns the result to the variable on the left.
Example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 8. 
Then (a -= 6)  
        (a = a - 6) 
        (a = 2).

“*=”: This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on right and then assigns the result to the variable on the left.
Example:
(a *= b) can be written as (a = a * b)
If initially value stored in a is 5.
Then (a *= 6) 
        (a = a * 6) 
        (a = 30).

“/=”: This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on right and then assigns the result to the variable on the left.
Example:
(a /= b) can be written as (a = a / b)
If initially value stored in a is 6.
Then (a /= 2) 
        (a = a / 2) 
        (a = 3)

“%=”: This operator is combination of ‘%’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on right and then assigns the result to the variable on the left.
Example:
(a %= b) can be written as (a = a % b)
If initially value stored in a is 6.
Then (a %= 2) 
        (a = a % 2) 
        (a = 0)

/* Program to illustrate Assignment operators */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
#define sf scanf
void main()
{
int a;
clrscr();
pf("\n enter a value :");
sf("%d",&a); 
a-=8;  
pf("\n Result is %d",a);
getch();
}

8.Ternary or Conditional Operator (? :)
Conditional operator is of the form 
Expression1 ? Expression2 : Expression3 . 
If the condition (Expression1) is True then we will execute and return the result of Expression2 otherwise 
if the condition (Expression1) is false then we will execute and return the result of Expression3.
We may replace the use of if..else statements by conditional operators. 

/* Program to illustrate Ternary or Conditional operators */

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
void main()
{
int a=10,b=5;
clrscr();
(a>b) ? pf("\the value of a is %d",a) : pf("\the value of b is %d",b);
getch();
}

9.Special Operators: There are two special operators in c they are 

1. Sizeof operator: sizeof is a much used in the C programming language. It is a compile time unary operator which can be used to compute the size of its operand. Basically, sizeof operator is used to compute the size of the variable. 

2. Comma Operator: The comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator. Comma acts as both operator and separator.

* Program to illustrate sizeof and comma operators */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define pf printf
void main()
{
int x,a=10,b=5;
float r;
double q;
char p;
clrscr();
x=(a=10,b=5,a+b);
pf("\n The value of the given comma operator is %d",x);
pf("\n The number of bytes occupied by given data type is  %d",sizeof(p));
pf("\n The number of bytes occupied by given data type is  %d",sizeof(a));
pf("\n The number of bytes occupied by given data type is  %d",sizeof(r));
pf("\n The number of bytes occupied by given data type is  %d",sizeof(q));
getch();
}
OUTPUT:

No comments:

Post a Comment

Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...