Search This Blog


Showing posts with label Operators. Show all posts
Showing posts with label Operators. Show all posts

Wednesday, September 18, 2019

#2 Opertaor in C


➤Assignment operator  :-

This operator is used to assign the value in variable .

Example:-

A=10
B=10.5
Name=”COc”

➤Multiple assignment operator :-

This operator is used to assign same value in multiple variables.

Example:-

a = b = c = 10;

➤Compund assignment  :-

  • Assignment  operator is used with arithmetic operator  and bitswise operator. This concept is called compound assignment operator.
  • It is also known as shortcut assignment operator.

Example:-

a = a+10   can be written as  a+=10
a = a *30 can be written as  a*=30

➤Increment operartor (++)  :-

This operator is used to increase the value of variable by 1.

⏩Prefix increment :-

  • Increment operator is used before operand. Eg:- ++a
  • Value of variable is incremented first,then new value is used in operation .

⏩Postfix increment :-

  • Increment operator is used after operand. Eg:- a++
  • Value of variable is used in the operation first, then incremented

Evaluation of prefix and and postfix :

Prefix
Postfix
1.       a=10, b=20
++a    a=11
++b    b=21
a=11
b=21

2.       a=10, b=20
p=++a    a=11
q=++b    b=21
a=11
b=21
p=11
q=21

3.       a=10, b=20
++a    a=11
++b    b=21
p=++a   a=12
q=++b   b=22
a=12
b=22
p=12
q=22



1.       a=10, b=20
a++   a=10
b++   b=20
a=11
b=21

2.       a=10, b=20
p=a++   a=10
q=b++   b=20
a=11
b=21
p=10
q=20

3.       a=10, b=20
a++    a=10
b++    b=20
p=a++    a=11
q=b++    b=21
a=12
b=22
p=11
q=21

➤Decrement operator :-

This operator is used to decrease the value of variable by 1.

⏩Prefix decrement :-

  • Decrement operator is used before operand. Eg:-  --a
  • Value of variable is decremented first,then new value is used in operation .

⏩Postfix decrement :-

  • Decrement operator is used after operand. Eg:-   a--
  • Value of variable is used in the operation first, then decremented.

➤Comma operator :-

  • This operator is used to separate multiple values within parenthesis sign “( )”.
  • In this case last value will be evaluated and assigned in variable.

Case1:-

             a=(10,20,30,40);

      a=40

Case2:- 

               a=10,20,30,40;
       a=10

➤Sizeof operator :-

This operator return the size of data type and variable in byte.

Syntax:-

              sizeof(data type variable)

example:-

·         sizeof(int)    2byte  
·         sizeof(float)   4byte
·         int a; sizeof(a);  2byte
·         sizeof(‘A’)   1byte
·         sizeof(“coc”)   4byte


Tuesday, September 17, 2019

Operator in C

Operator is a special symbol which is used to perform particular operation on operands.
example:- a+b-c   
              here: +,- are operators and a,b,c are operands.

There are some operators in C :-

  1. Arithmetic operator
  2. Relational operator
  3. Logical operator
  4. Assignment operator
  5. Multiple assignment operator
  6. Compound assignment operator/shortcut assignment
  7. Increment
  8. Decrement
  9. Comma operator
  10. Sizeof operator
  11. Bitwise operator
  12. Unary operator
  13. Binary operator
  14. Ternary operator/conditional

➤Arithmetic operator :-

This operator is used to perform arithmetic operation as addition, subtraction, multiplication,etc.
Operator
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Remainder / Modulo division

example:-

10 / 4=2
10.0 / 4=2.5
10 / 4.0=2.5
10 % 4=2
4 % 10=4
-10 % 4=-2
10 % -4=2
-10 % -4=-2
-30.45 % 2.45= error

Note:1 If the value of operand is less than the value of second operand then remainder must be the value of first operand.
Note:2 The sign of remainder depends on only the sign of first operand.
Note:3 Real type operands cannot be allowed with modulo division.

➤ Relational operator :-

 This type of operator is used to compare between two numbers.
It returns Boolean value as zero(false) or one(true).
Operator
Meaning
< 
Less than
> 
Greater than
<=
Less than or equal to
>=
Greater than or equal to
!=
Not equal to
==
Equal to

example:-

20>=20 ➔ 1
30 > 40 ➔ 0
4 != 5   ➔ 1
4 == 6 ➔ 0
30 < 40 ➔ 1

➤ Logical operator :-

This operator is used to combine more than one condition.
It also returns Boolean value.
Operator
Meaning
&&
Logical AND
||
Logical OR
!
Logical NOT

⏩Logical AND(&&) :-

It returns true if all combined condition are true otherwise returns false.

example:-

  1. 20 >= 20 && 30 > 50 && 4 != 6 ➔ false(0)
  2. 4 != 6 && 4>3 && 4 == 4 ➔ true(1)

⏩Logical OR(||) :-

It returns true if any combined condition is true otherwise returns false.

example:-

  1. 20 >= 20 || 30 > 50 || 4 != 6 ➔ true(1)
  2. 40 != 40 || 40 > 50 ➔ false(0)

⏩Logical NOT(!) :-

It returns true if condition is false otherwise returns false.

example:-

  1. !(40 >50) ➔ true
  2. !(30 > 50 && 4 != 6) ➔ true
  3. !(40 != 60 || 30 > 60) ➔ false