Operators in c programming

An operator is a symbol  (+,-,*,/) that directs the computer to perform certain mathematics and is usually used to manipulate data and variables.

Ex: a+b


Expressions:


Combination of Operators and Operands.


Example:           ( 2 * y + 5)

                                 

                                  *,+ = operators

                           2,y,4 = operands











Operators in C:


  1. Arithmetic operators

  2. True or false table

  3. Relational operators

  4. Logical operators

  5. Assignment operators

  6. Increment and decrement operators

  7. Conditional operators

  8. Bitwise operators

  9. Special operators




Arithmetic operators:


Operator                    Example                    Meaning

                 +                                      a+b                             Addition –unary

                 -                                       a-b                              Subtraction- unary

                 *                                       a*b                              Multiply

                  /                                        a/b                             Division

                 %                                      a%b                           Modulo division







Operator                   Meaning

                           <                                                                             Is less than
                           <=                                                                           Is less than or equal to 
                           >                                                                             Is greater than 
                           >=                                                                           Is greater than or equal to
                           ==                                                                           Equal to
                           !=                                                                            Not equal to







Logical Operators:



Operator                              Meaning

                           &&                                                                        Logical  AND
                          
                            ||                                                                          Logical OR

                            !                                                                           Logical NOT




Logical expression or a compound relational expression- 

An expression that combines two or more relational expressions 

Ex: if (a==b && b==c)



Truth Table:

                           Value of the expressions

 a          b                   a&&b           a||b

   0                    0                                          0                                1
   0                    1                                          0                                1
   1                    0                                          0                                1
   1                    1                                          1                                1


                            





Assignment operators :


Syntax: 

v op = exp; 

Where v = variable, 

op = shorthand assignment operator 

exp = expression 

Ex: x=x+3 x+=3



Increment & Decrement Operators:


 C supports 2 useful operators namely :

1.      Increment ++ 

2.      Decrement – operators 

  • The ++ operator adds a value 1 to the operand 

  • The – operator subtracts 1 from the operand ++a or a++ --a or a--



Rules for ++ & -- operators:


1. These require variables as their operands 

2. When postfix either ++ or – is used with the variable in a given expression, the expression           is evaluated first and then it is incremented or decremented by one

3. When prefix either ++ or – is used with the variable in a given expression, it is incremented or decremented by one first and then  the expression is evaluated with the new value





Examples for ++ & -- operators :


Let the value of a =5 and b=++a then

a = b =6 Let the value of a = 5 and b=a++ then 

a =5 but b=6 

i.e.: 

1. a prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left 

2. a postfix operator first assigns the value to the variable on left and then increments the operand.



Conditional operators:


Syntax: 

exp1 ? exp2 : exp3 

Where exp1,exp2 and exp3 are expressions 

Working of the ? Operator: 

Exp1 is evaluated first, if it is nonzero(1/true) then the expression2 is evaluated and this becomes the value of the expression, 

If (expression is false) exp1 is false(0/zero) exp3 is evaluated and its value becomes  the value of the expression Ex: m=2; n=3 r=(m>n) ? m : n;




Bitwise operators:




Operator                              Meaning

                           &&                                                                        Bitwise  AND
                          
                            |                                                                          Bitwise OR

                            ^                                                                          Bitwise exclusive OR 

                           <<                                                                         Shift left

                           >>                                                                         Shift right




Special operators

(used in programming languages): 


  1.      Comma operator ( ,) 

  2.      sizeof operator – sizeof( ) 

  3.      Pointer operators – ( & and *) 

  4.      Member selection operators – ( . and ->)

Post a Comment

1 Comments