Operator in C/C++

Operator:-

  • Operator is a symbolic name which is used to manipulate data.
  • It is the symbol use to perform calculation.
  • It perform to operand.
  • EX:- +,- ,* ,% ,/ ,&
Operator

Types of operator:-

There are two types of operator

1.According to operand:-

  • Operand is the value on which the operator perform the calculation.
  • EX:- See the upper image

2.According to operand there are three types of operator.

A. Unary operator:-

  • Operator that takes only one value is called as unary operator.
  • EX:-Negation operator(-),increment(++),Decrement(–)

B. Binary operator:-

  • Operator that takes two value is called as Binary operator.
  • EX:- + ,- , * ,< ,>,

C. Ternary Operator:-

  • Operator that take three value is called as Ternary Operator.
  • EX:-Conditional operator(?)

2.According to the calculation:-

  • On the basic of calculation there are following types of operator.

[i] Arithmetic operator

[ii] Bitwise operator

[iii] Conditional operator

[iv] Decrement & Increment operator

[v] Logical operator

[vi] Assignment operator

[vii] Relational operator

[viii] Special operator

a. Comma(,) operator

b. Size of() operator

[I] Arithmetic operator:-

  • This operator is used to perform arithmetic operator.
operatorMeaningExampleResult
+Addition20+222
Subtraction20-218
*Multiplication20*240
/Division21/210
%Remainder21%21

[ii] Bitwise operator:-

  • This operator is used to perform operation at binary digit level.
operatorMeaning
<<Shift the bit to Left
>>Shift the bit to Right
 ~One’s compliment
|Logical Bitwise OR
&Logical Bitwise AND
Logical Bitwise EX-OR

[iii] Conditional operator:-

  • This operator is used to check a condition and select a value depending on the value of condition.

SYNTAX:-

Variable=(condition)? value1: value2,during the operation if condition be true. It gives value1 as output otherwise it gives value2 output.

EX:-

INT a=7 b=10 , C

1st (a<b)? a:b,

c=7

2nd c=(a>b)?a:b,

c=10

[iv] Decrement & Increment operator:-

A. Decrement operator:-

  • This operator is used to reduced the value of integer variable by 1(one)
  • It is denoted by(–)
  • There are two types of decrement operator.

a. Predecrement operator:- This operator is used to reduce the value of variable before calculation.

EX:-

INT a=7 b=8 C

(i) C=(–a)+b

=6+8 =14

(ii) c= (–a)+(–b)

= 6+7

=13
b. Post-Decrement:- This operator is used to reduce the value of variable after calculation by 1(one).

EX:-

INT a=6 b=3 C,

C=(a–)+(b–)

=6+3

=9

Hance

a=5

b=2

B. Increment operator:-

  • This operator is used to increase the value of integer variable by one
  • It is denoted by(++)
  • There are two types of increment operator.

a. Pre-increment operator:- This operator is used to increase the value of of variable before calculation by 1(one).

EX:- INT a=5 , b=3 C,

C=(++a)*(++b)

C=6*4

=24

b. Post-increment:- This operator is used to increase the value of variable after calculation by1(one).

EX:-

a=7 ,b=4 C,

C=(a++)-(b++)

=7-4

=3

Hance

a=7

b=5

[v] Logical operator

  • This operator is used to perform logical operation.
operatorMeaningExampleResult
&&Logical AND(14>2)&&(14<2)F
||Logical OR(14>2)||(14<2)T
!Logical NOT!(14<2)T

[VI]Assignment operator

  • Assignment operator are used to perform arithmetic operation while assigning a value to a variable.
operatorExampleCondition(M=20)Result
+=M+=10M=M+1030
-=M-=10M=M-1010
*=M*=10M=M*10200
/=M/=10M=M/102
%=M%=10M=M%100

[Vii] Relational operator:-

  • This operator is used to compare the value of the operands.
operatorMeaningExampleResult
<less than5<2F
>Greeter than7>2T
<=less than or equal5<=2F
>=Greeter than or equal5>=2T
==Equal4==2F
!=Not equal4!=2T

[Viii] Special operator

a. Comma(,) operator:-

  • This operator is used to separate two variable, expression
  • EX:- INT a,b,c

b. Size of () operator:-

  • This operator is used to find the size of data type in this memory.
  • EX:-INT a,b
  • Float p,q
  • Char x,y,z

HERE:-

a=size of(x)=1 Byte

b=Size of (p)=4 Byte

Relate link

ALL THE BEST TO THE ENGINEER 👀 THANKS FOR VISIT csestudy247.com Please comment any Doubt Thanks