google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Operator / Types of Operator in C

Operator / Types of Operator in C

1
What is Operator?

Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations.

Operands are variable or expression which are used  in conjunction with operators to evaluate the expression.

 Combination of operands and operators from an expression.


Expression in C

Types of Operators

  1.  Arithmetic Operators
  2.  Logical Operators
  3.  Relational Operators
  4.  Assignment Operators
  5.  Conditional Operator
  6.  Increment / Decrement Operators
  7.  Bitwise Operators/Shift Operators
  8. sizeof operator

1. Arithmetic Operator?

An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).


Operator
Meaning of Operator
+
Addition
-
Subtraction
/
Division
*
Multiplication
%
Remainder after Division (Modulo   Division)


Properties of Arithmetic Operators 


  •  These are “Binary operator” and they take two operands.
  •  The Operands must be “Numeric Numbers”.
  •  When both operands are integer, then result is integer. But when one of them is floating point and other is integer then the result is floating point.
  •   Except “%” operators, all the arithmetic operators can be used with any type  of numeric operands (either operands are integer and floating point), while  “%” operator can be used with only integer data type.
Example of Arithmetic Operators 

 #include<stdio.h>
#include<conio.h>
 void main()
{
 int a=4,b=9,c;
 c=a+b;
 printf(“The sum is =%d”, c);
 getch();
}
Output:
The sum is =13
Conditional Operators 

Java language provides “?” operator as a conditional operator.
 These are “ Ternary Operators” as they take three operands.

Syntax

 Expression = Condition  ? Expression 1 : Expression 2 ;
The statement above is equivalent to:
 if (condition)
Expression 1
 else 
Expression 2

 The operator “?” works as follows.
Expression is evaluated first. If it is nonzero(true), then the expression 1 is evaluated and became the value of expression.
If expression is false, expression 2 is evaluated and its value becomes the value of expression.
It is necessary only one is evaluated (either expression 1 or expression 2).
So it is clear that conditional operators (Ternary Operator) used as the “if..else” statement 

Example Conditional Operators 

 Using as if .. Else..
 if(total>60)
  grade=‘P’;
 else
   grade=‘F’;
Using Conditional Operator..
 total=total>60? grade=‘P’ : grade=‘F’; or total=total>60?’P’ :’F’;

Assignment Operators 

An assignment operator is used for assigning a value to a variable. The most 
common assignment operator is “ = “.


Assignment operator



Operator
Example
Same as
=
a = b
a = b
+=
a += b
a = a+b
-=
a -= b
a = a-b
*=
a *= b
a = a*b
/=
a /= b
a = a/b
%=
a %= b
a = a%b

Example Assignment Operators 

#include <stdio.h>
#include <conio.h>
void main()
{
    int a = 5, c;
    c = a;      // c is 5
    printf("c = %d\n", c);
    c += a;     // c is 10 
    printf("c = %d\n", c);
    c -= a;     // c is 5
    printf("c = %d\n", c);
    c *= a;     // c is 25
    printf("c = %d\n", c);
    c /= a;     // c is 5
    printf("c = %d\n", c);
    c %= a;     // c = 0
    printf("c = %d\n", c);
    getch();
}
Output:
c = 5 
c = 10 
c = 5 
c = 25 
c = 5 
c = 0

Relational Operators 

 A relational operator checks the relationship between two operands. If the relation is true, it 
returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.


Operator
Meaning of Operator
Example
==
Equal to
5 == 3 is evaluated to 0
> 
Greater than
5 > 3 is evaluated to 1
< 
Less than
5 < 3 is evaluated to 0
!=
Not equal to
5 != 3 is evaluated to 1
>=
Greater than or equal to
5 >= 3 is evaluated to 1
<=
Less than or equal to
5 <= 3 is evaluated to 0


Example Relational Operators 

#include <stdio.h>
#include <conio.h>
void main()
{
    int a = 5, b = 5, c = 10;
    printf("%d == %d is %d \n", a, b, a == b);
    printf("%d == %d is %d \n", a, c, a == c);
    printf("%d > %d is %d \n", a, b, a > b);
    printf("%d > %d is %d \n", a, c, a > c);
    printf("%d < %d is %d \n", a, b, a < b);
    printf("%d < %d is %d \n", a, c, a < c);
    printf("%d != %d is %d \n", a, b, a != b);
    printf("%d != %d is %d \n", a, c, a != c);
    printf("%d >= %d is %d \n", a, b, a >= b);
    printf("%d >= %d is %d \n", a, c, a >= c);
     printf("%d <= %d is %d \n", a, b, a <= b);
    printf("%d <= %d is %d \n", a, c, a <= c);
    getch();
}
Output:
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1 



Logical Operators 

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in Java programming.


Operator
Meaning
Example
&&
Logical AND. True only if all operands are true
If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0.
||
Logical OR. True only if either one operand is true
If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1.
!
Logical NOT. True only if the operand is 0
If c = 5 then, expression !(c==5) equals to 0.

Example Logical Operators 

 Example Logical Operators 

#include <stdio.h>
#include <conio.h>
void main()
{
    int a = 5, b = 5, c = 10, result;
    result = (a == b) && (c > b);
    printf("(a == b) && (c > b) is %d \n", result);
    result = (a == b) && (c < b);
    printf("(a == b) && (c < b) is %d \n", result);
    result = (a == b) || (c < b);
    printf("(a == b) || (c < b) is %d \n", result);
    result = (a != b) || (c < b);
    printf("(a != b) || (c < b) is %d \n", result);
    result = !(a != b);
    printf("!(a == b) is %d \n", result);
    result = !(a == b);
     printf("!(a == b) is %d \n", result);
    getch();
}
Output:
(a == b) && (c > b) is 1 
(a == b) && (c < b) is 0 
(a == b) || (c < b) is 1 
(a != b) || (c < b) is 0 
!(a != b) is 1 
!(a == b) is 0 

Increment/Decrement Operators 

Java programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.

 Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.

Types of Increment/Decrement Operators 

There are Two types Increment / Decrement Operator


  1. Pre (Increment/Decrement)
  2. Post (Increment/Decrement)


Suppose you use ++ operator as prefix like: ++var. The value of var is incremented by 1 then, it returns the value.

Suppose you use ++ operator as postfix like: var++. The original value of var is returned first then, var is incremented by 1.

Example Increment Operators 


#include <stdio.h>
#include <conio.h>
void main()
{
  int var=5;
  
  // 5 is displayed then, var is increased to 6.
  printf("%d\n",var++);  
  
  // Initially, var = 6. It is increased to 7 then, it is displayed.  
  printf("%d",++var);    
 
getch();

}


Output:

5
7


Example Decrement Operators 


#include <stdio.h>
#include <conio.h>
void main()
{
  int var=5;
  
  // 5 is displayed then, var is increased to 4.
  printf("%d\n",var--);  
  
  // Initially, var = 4. It is increased to 3 then, it is displayed.  
  printf("%d",--var);    
 
getch();

}



Output:

5
3


Bitwise Operators 

 In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction, multiplication and division are done in bit-level. To perform bit-level operations in Java programming, bitwise operators are used. 


Operators
Meaning of operators
&
Bitwise AND
|
Bitwise OR
^
Bitwise XOR
~
Bitwise complement
<< 
Shift left
>> 
Shift right

Bitwise AND Operators 

The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0.
 Let us suppose the bitwise AND operation of two integers 12 and 25.


12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25
     00001100
&   00011001
      ________
      00001000 = 8 (In decimal)


Example Bitwise AND Operators 


#include <stdio.h>
#include <conio.h>
void main()
{
    int a = 12, b = 25;
    printf(“Result is= %d", a&b);
    getch();
}
Output:
Result is=8


Bitwise OR Operators 

The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In Java Programming, bitwise OR operator is denoted by |.


12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
  00001100
| 00011001
  ________


                              00011101  = 29 (In decimal)


Example Bitwise OR Operators 


#include <stdio.h>
#include <conio.h>
int main()
{
    int a = 12, b = 25;
    printf(“Result is= %d", a|b);
    getch();
}

Output:

Result is=8

Bitwise XOR Operators 

The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.


12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
  00001100
^ 00011001
  ________


                               00010101  = 21 (In decimal)

Example Bitwise XOR Operators 

 #include <stdio.h>
#include <conio.h>
int main()
{
    int a = 12, b = 25;
    printf(“Result is= %d", a^b);
    getch();
}
Output:
Result is=21

Bitwise Complement Operators 

 Bitwise compliment operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by ~.


35 = 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
  ________


                                 11011100  = 220 (In decimal)

 2's Complement
Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus 1. For example:


Decimal         Binary                                 2's complement
   0              00000000                              -(11111111+1) = -00000000 = -0(decimal)
   1              00000001                              -(11111110+1) = -11111111 = -256(decimal)
   12            00001100                              -(11110011+1) = -11110100 = -244(decimal)
   220          11011100                              -(00100011+1) = -00100100 = -36(decimal)



Note: Overflow is ignored while computing 2's complement. 

The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.
Bitwise complement of any number N is -(N+1). Here's how:


bitwise complement of N = ~N (represented in 2's complement form)


2'complement of ~N= -(~(~N)+1) = -(N+1)

Twist in bitwise complement operator in Java Programming

The bitwise complement of 35 (~35) is -36 instead of 220, but why?

For any integer n, bitwise complement of n will be -(n+1). To understand this, you should have the knowledge of 2's complement.


Example Bitwise Complement Operators 


#include <stdio.h>
#include <conio.h>
void main()
{
    printf(“Result is= %d\n",~35);
    printf("Output = %d\n",~-12);
    getch();
}
Output:
Result is =-36

Result is =11


Shift Operators 

There are two shift operators in Java programming:


  1. Right shift operator
  2. Left shift operator.



Right Shift Operators 

Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>.


212 = 11010100 (In binary)
212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000


212>>0 = 11010100 (No Shift)

Left Shift Operators 

Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<.


212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 =11010100 (Shift by 0)


212<<4 = 110101000000 (In binary) =3392(In decimal)

Example Shift Operators 


#include <stdio.h>
#include <conio.h>
void main()
{
    int num=212, i;
    for (i=0; i<=2; ++i)
        printf("Right shift by %d: %d\n", i, num>>i);
     printf("\n");
     for (i=0; i<=2; ++i
        printf("Left shift by %d: %d\n", i, num<<i);    
    
     getch();

}

sizeof Operators 

The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).

#include <stdio.h>
#include <conio.h>
void main()
{
    int a;
    float b;
    double c;
    char d;
    printf("Size of int=%lu bytes\n",sizeof(a));
    printf("Size of float=%lu bytes\n",sizeof(b));
    printf("Size of double=%lu bytes\n",sizeof(c));
    printf("Size of char=%lu byte\n",sizeof(d));

    getch();
}

Output:

Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte


Watch Video Lecturer Here--


Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

  1. Throw in employee salaries, overhead, and marketing expenses—which Vixio mentioned could make up over 1xbet korea 50% of total revenue alone—and online sports activities playing simply isn’t a moneymaking business at this level. People get pleasure from each half in} in sports activities properly as|in addition to} being a fan, cheering on their favorite team. Not only do fans simply get pleasure from watching however they also like to put wagers on the events. Sports betting really has a protracted historical past and has been around for hundreds of years}.

    ReplyDelete

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top