OPERATORS


An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables.
OPERAND: It is an entity on which an operator acts.
UNARY OPERATOR: It requires only a single operand.
BINARY OPERATOR: It requires two operands to act.
C operators can be classified into number of categories. They include:

  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 operators

are 2 types of arithmetic operators.

  1. Binary operator and
  2. Unary operator.

These operators are commonly used in most of computer languages.
Binary operator: This is used for numerical calculations between 2 constants values. Unary operator: Unary operators are increment operator (++), decrement operator (--) and minus (-).
Binary operators

  1.   +    Addition    4+7=11
  2.   -    Subtraction    7-4=3
  3.   *    Multiplication    7*4=28
  4.   /    Division    10/5=2
  5.   %    Modular division    13%3=1(reminder)
Unary operators
  1.   -    Minus    4+7=11
  2.   ++    Increment    7-4=3
  3.   --    Decrement    7*4=28
  4.   &    Gives the size of operator    10/5=2

Example for unary minus : int x = -10;
There is no unary plus in ‘C’.
Even though, a value assigned with plus sign is valid. i.e int x=+50
Here + is valid but in practice, this sign should not be attached.
Program:

 #include < stdio.h>
 #include < conio.h>
void main()
{
       int a, b ;
       clrscr();
       printf("Enter two numbers");
       scanf("%d %d’, &a, &b);
       printf("\n Addition of %d & %d is %d", a ,b .a + b);
       printf("\n Subtraction of %d & %d is %d", a ,b, a-b);
       printf("\n Multiplication of %d & %d is %d", a, b, a*b);
       printf("\n division of %d & %d is %d", a ,b, a/b);
       printf("\n remainder of %d & %d is %d", a, b, a % b);
       getch();
}
                

Relational operator:

These operators provide the relationship between the two expressions. If the relation is true then it returns a value 1 otherwise o for false relation .
Relational operators

  1.   >    Greater than    5>4
  2.   <    Less than    3<2
  3.   <=    Less than or equal to    10<=10
  4.   >=    Greater than or equal to    11>=5
  5.   ==    Equal to    2==3
  6.   !=    Not Equal to    3!=3
Program:
#include < stdio.h>
#include < conio.h>
void main()
{ 
  int  a, b ;
  clrscr();
  printf("Enter the values in a and b");
  scanf("%d%d", &a, &b);
  printf("Value of a>b is %d" ,a>b);
  printf("Value of a < b is %d", a < b);
  printf("Value of a<=b is %d", a <=b);
  printf("value of a>=b is %d",  a >=b);
  printf("Value of a!=b is %d", a!=b);
  printf("Value of a==b is %d", a = =b);
  getch();
}
                

Logical operators:

The logical relationship between two expressions is checked with the logical operators. Using these operators two expressions can be joined. After checking the conditions it provides logical true (1) or false(0) status. The operands could be constants variables and expressions.
Logical operators

  1.   &&    Logical AND    4>2&77<9
  2.   ||    Logical OR    4>2||7<9
  3.   !    Logical NOT    7!=7
Exp 1 Exp 2 Exp1 && Exp2
T T T 
T F F 
F T F 
F F F 

Where Exp = true    ! Exp = false
Exp = false   ! Exp= true

Exp 1 Exp 2 Exp1 || Exp2
T T T 
T F T 
F T T 
F F F 

        
Program:
#include < stdio.h>
#include < conio.h>
void main()
{
     int x, y;
     clrscr();
     printf("Enter the vales in  x and y\n");
     scanf("%d%d", &x, &y);
     printf("Value of (x= =y)&&(x>y) is %d",(x= =y)&&(x>y));
     printf("Value of (x==y)||(x>y) is %d", (x==y)||(x>y));
     printf("Value of !((x>y)&&(x<=y)) is %d", !((x>y)&&(x<=y)));
     getch();
}
                

Assignment operators:

Values can be assigned to variables using the "assignment operator" (=) as follows:
Syntax:Variable-name=constant;
a=10;
b=20;
It is also possible to assign a value to a variable at the time the variable is declared. This taken the following form:
Syntax:Data type variable name =constant;
int a =20;
An assignment statement implies that the value of variable on the left side of ‘equal sign’ is set equal to the value of the quantity (or expression) on the right.
The statement i=i+1; means that the new value of ’i’ is equal to b/d value of i+1.
The process of giving initial values to variables is called initialization. ’C’ permits the initialization of more than one variables in one statement using multiple assignment operators.
P=q=s=0;
Compound assignment operators (or) Shorthand assignment operators:
+=,-=,*=,/=,%=

Ex: a+=5
b-=10
Assignment operators are used to assign the result of an expression to a variable. We have seen the usual assignment operator, ‘=’.C has a set of ‘shorthand’ assignment operators of the form
V op = exp;
Where v is a variable, exp is an expression and op is a C binary arithmetic operator. The operator op = is known as the shorthand assignment operator.
The assignment statement
V op = exp;
is equivalent to
v = v op (exp);
With v is evaluated only once. Consider an example
x+=y+1;
This is same as the statement
x=x+(y+1);
The shorthand operator += means ‘add y+1 to x’ or ‘increment x by y+1’.For y=2,the above statement becomes
x+=3;
and when this statement is executed,3 is added to x. If the old value of x is 5,then the new value of x is 8.some of the commonly used shorthand assignment operators are illustrated in the following table:

Statement with simple     Statement with
Assignment operator     shorthand operator     
a=a+1         a+=1
a=a-1         a-=1
a=a*(n+1)       a*=n+1
a=a/(n+1)       a/=n+1
a=a %b              a %=b

The use of shorthand assignment operators has 3 advantages:
  1. What appears on the left-hand side need not be repeated and therefore it becomes easier to write.
  2. The statement is more concise and easier to read.
  3. The statement is more efficient.

Increment & Decrement operators

‘C’ has 2 very useful operator not generally found in other language. They are increment & decrement operators (++ & --).The operator ++ add 1 to operand while -- subtracts 1.Both are unary operators and take following form.
++a & a++;
--a & a--;
(Pre decrement) & (post decrement)
++a; is equivalent to a=a+1;(or a+=1)
--b; is equivalent to b=b-1;(or b-=1)
We use the increment & decrement statements in for & while loops extensively.
Ex:

 
1.a=10;
  ++a (or) a++
  a=11
2. int a=10,b;
  b=++a;
  a will contain  11
  b also will contain 11
3.int a=10,b
  b=a++
  a=11;
  b=10;
Decrement
1. a =10;
  --a  (or)a--
  a=9
2 .int a=10,b;
  b=--a;
  b=9   
  b=9
3. int a=10,b;
  b=a--;
  a=9;
  b=10;


Bitwise Operators

’C’ has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level.
These operators may not be applied to float (or) double.
These are classified into 2 types
1. Bitwise logical operators: These operators are used for the Bit-wise logical decision making.
&-Bitwise logical AND
| -Bitwise logical OR
^-Bitwise logical exclusive OR
program

#include < stdio.h>
#include < conio.h>
void main()
{
    int a,b;
    clrscr();
    printf("enter a,b");
    scanf("%d%d",&a,&b);
    printf("\n a&b = %d",a&b);
    printf("\n a|b =%d",a|b);
    printf("\n a^b =%d",a^b);
    getch();
}
                
Bit-wise shift operators
Shift operator to be binary patterns and shift the bits to the left or right.
<<-left shift;
>>-right shift
Ex:
A=4-100
int b,c;
PROGRAM
#include < stdio.h>
#include < conio.h>
void main()
{
  int a,b,c;
  clrscr();
  c=(a=10,b=20,a+b);
  printf("\n a=%d",a);
  printf("\n b=%d",b);
  printf("\n c=%d",c);
  getch();
}
                

THE SIZEOF OPERATER

The size of is a compile time operator & when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier Ex: m=sizeof (sum);
n=size of (float); -4
k=size of(23); -2
Ex: int a, b;
Size of (a)-2
Size of (b)-2
Size of(c)-2
PROGRAM

#include < stdio.h>
#indlude < conio.h>
void main()
{ 
   int a,b;
   clrscr();
   printf("%d",sizeof(a));
   printf("%d",sizeof(b));
   printf("%d",sizeof(a+b));
   getch();
}
                

Copyright © 2018-2020 TutorialToUs. All rights reserved.