Tuesday, 28 March 2017

operator in c | How to use operator in c language

Operators are the symbols which instruct compiler about which operation will perform in this line of code.There are eight categories in Operators C.

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Conditional Operators
  5. Assignment Operators
  6. Increment and Decrement Operators
  7. Bitwise Operators
  8. Special Operators

Arithmetic Operators :


An arithmetic operator plays mathematical operations which include addition, subtraction and multiplication on numerical values (constants and variables).

 Operator        Meaning of Operator
+addition or unary plus (Use when we add two or more then two numbers)
-subtraction or unary minus (Use when we Subtract two or more then two numbers)
*multiplication (Use when we Multiply two or more then two numbers)
/division (Use when we divide two or more then two numbers)
%remainder after division(when we need to find remainder) 

Relational Operators :

A relational operator checks the relationship among  operands. If the relation is proper, it returns 1; if the relation is fake, it returns zero.

OperatorMeaning of OperatorExample
==Equal toWhen we find both values are equal or not
( a == b returns 0 )
>Greater thanWe use when we check 'a' Greater than 'b'
( a > b returns 1 )
<Less thanWe use when we check 'a' Less than 'b'
a < b returns 0
!=Not equal toWe use when we check 4 not equal to 3
( 4 != 3 returns 1 )
>=Greater than or equal toWe use when we check 5 Greater than or equal to 3
( 5 >= 3 returns 1 )
<=Less than or equal toWe use when we check 5 Less than or equal to 3
 ( 5 <= 3 return 0 )


Logical Operators :


       &&              AND
        ||        OR
        !       NOT    


AND ( && )

( Age > 18  &&  Age < 50 )

In Logical Operator AND (&&) when Both conditions are true then execute the statement.

OR ( || )

( Age > 18  ||  Age < 50 )

In Logical Operator OR ( || ) when only one or both conditions are true then execute the statement.

NOT ( ! )

( Age > 18  !  Age < 50 )

In Logical Operator NOT ( ! ) first condition must be true and second false then execute the statement.

Conditional Operators :

In Conditional Operator we use ( ? ) and ( :: ) in pair. But how ? see in this line below.

                                        conditional_Expression ? expression1 : expression2

If conditional is true, expression1 is evaluated.
If conditional is false, expression2 is evaluated.

int a=5;
int b=4;
                                             C = ( a > b )? a : b ;

If Conditional_Expression is true then Expression ( a ) will be Evaluate if  Conditional_Expression is 
false then Expression ( b ) will be evaluate.

So, if Expression ( a ) is true then C= 5 else C=4 . Conditional Operator behave same like if else. 

Assignment Operators :

In Program we use Assignment Operator to assign the value in variable or store the result in variable. 

                                                Int a = 100;
Here we used Assignment Operator to assign the value in variable ( a ). Assignment Operator assign the right side value in left side.

Increment And Decrement Operators :

Increment And Decrement Operator simply increase or decrease the value. We can use this Operator by two ways Pre Increment And Decrement and Post Increment And Decrement Operator.
                                                                    Int a = 10;

If we use Increment Operator ( a++ ) then a = 10 and if we use Decrement Operator ( a-- ) then a = 9.

Pre    ++a , --a
Post  a++,  a--


                                          int a =100, b;


Post Increment :

b = a++;  In this Expression first value of ( a ) assign to ( b ) then Increment Operator apply

so a = 101 and b = 100

Pre Increment :

b = ++ a ; In this Expression first Increse the value of ( a ) then assign the value in ( b )

so in this case a= 101 and b= 101.

Bitwise Operators :

        &               AND
        ||         OR
        ^   Exclusive  OR       
        <<        left shift
        >>       right shift   

All through computation, mathematical operations like: addition, subtraction, addition and division are converted to bit-stage which makes processing quicker and saves strength. Bitwise operators are used in C programming to perform bit-level operations.

Special Operators :

Sizeof operator tells how much memory occupy this operand it shows in Byte.

int a = 100;

c= Sizeof (a);

So Sizeof operator return 2 because ( a ) has Integer data types And Integer takes 2 Bytes in memory. We can use Sizeof Operator for finding the size of Array Structure etc.




No comments:

Post a Comment