C Operators

Operators & Expressions:


‘C’ language supports a rich set of built-in operators. An operator indicates an operation to be performed on data. The operator is a special symbol, which denotes some computation.
Example:   a+b  :  where ‘+’ denotes operator to perform addition and a, b are operands.
            

(i)  Arithmetic operators:  

 

  There are five main arithmetic operators in ‘C’. They are ‘+’ for additions, ‘-' for subtraction, ‘*’ for multiplication, ‘/’ for division and ‘%’ for remainder after integer division. This ‘%’ operator is also known as modulus operator. The + and  - operators can be used as unary and binary operators, where as other operators are binary operators.
Operands can be integer quantities, floating-point quantities or characters. The modulus operator requires that both operands be integers & the second operand be nonzero. Similarly, the division operator (/) requires that the second operand be nonzero, though the operands need not be integers. Division of one integer quantity by another is referred to as integer division. With this division the decimal portion of the quotient will be dropped. If division operation is carried out with two floating- point numbers, or with one floating point number. & one integer, the result will be a floating-point quotient.
Example: 
  int x=20; and  int y=10;
         x+y=30
         x-y=10
         x*y= 200 
         x/y= 2   
         x %y= 0

(ii)  Relational operators:


   Relational operators are symbols that are used to test the relationship between two variables, or between a variable and a constant. All these operators are binary operators. ‘C’ has six relational operators as follows:

1. > greater than    
2. <  less than  
3.  != not equal to    
4. >= greater than or equal to
5. <=  less than or equal to 
6.  = =equal to
These relational operator are used to form logical expression representing condition that are either true or false. The resulting expression will be of type integer, since true is represented by the integer  ‘1’  and false is represented by the value  ‘0’. Let us understand operations of these relational operators with the help of an 

Example:    

 int  x=2, y=3, z=4;
x<y true 1, (x+y) >=z true 1, (y+z)>(x+7) false 0, z!=4 false 0, y = =3 true  1

(iii)  Logical/Boolen operators: 

  
 There are three logical operators in C language, they are and, or & not. They are represented by && , ||, ! respectively. These operators are referred to as logical and, logical or, logical not (logical negation) respectively. The ! operator is unary operator, where as &&, || operators are used as binary operators.The result of a logical and operation will be true only if both operands are true, whereas the result of a logical or operation will be true if either operand is true or if both operands are true. The result of a logical not operation will be true  if  operand is  false and vice-versa. The results are shown in the truth table.


A
B
A&&B
A||B
!A
0
0
0
0
1
0
1
0
1
1
1
0
0
1
0
1
1
1
1
0

The logical operators act upon operands that are themselves logical expressions. The logical operators are used to design complex relations and to find the logical relationship between two values.
Example: Suppose x=7, y=5.5, z= 'w'
The Logical expressions using these variables are as follows:
Expression Interpretation Value
(x>=6) &&(z= =’w’) true 1
(x>=6) (y = =119) true 1
(x<=6) && (z=='w') false 0

(iv) Assignment operators: 


There are several different assignment operators in C. All of them are used to form assignment expression, which assign the value of an expression to an identifier. The most commonly used assignment operator is =. The assignment operator is a binary operator. The assignment expressions that make use of this operator are written in the form:
                        
          Identifier = expression.
Identifier 1= identifier 2 = - - - - = expression     are allowed in ‘C’.
In such situations, the assignments are carried out from right to left.
Example :  X = 10;     It will cause the integer value 10 to be assigned to  x .
 X = y = 10;  It will cause the integer value 10 to be assigned to both x and y.

Compound Assignment Operators:


  ‘C’ also contains the five additional assignment operators +=, -=, *=, /= & %=. called as compound assignment operators.
expression1 + = expression2;   is equal to   expression1= expression1 + expression2;
Example: sum+=x;    is equal to      sum=sum + x;

(v)  Bitwise operators:


 The ‘C’ language includes operators to manipulate memory at the bit level. This is useful for writing low-level hardware or operating system code.  The bitwise operations are typically used with unsigned types. The bit-wise negation operator is unary, where as all operators are binary operators.
~  Bitwise Negation (unary) – changes (flips) form 0 to 1 and 1 to 0.
&  Bitwise And
 |  Bitwise Or
 ^  Bitwise Exclusive Or

          >>  Right Shift by RHS (divide by power of 2)   

   Ex:   x >> y (for positive int same as x/2y)

<<  Left Shift by RHS (multiply by power of 2)  
 Ex: x << y (for positive int same as x*2y)

Note:  Do not confuse the Bitwise operators with the logical operators. The bitwise operators are one character wide (&, |) while the logical connectives are two characters wide (&&, ||). The bitwise operators have higher precedence than the logical operators.

Truth table for bit-wise operators:





Examples:






Left shift(<<) and Right Shift (>>)Operators:


 The bitwise shift operators ‘<<‘ and ‘>>’ shift all bits of the left-hand operand either left or right by the number of bits specified in the right-hand operand.
The ‘<<‘operator is fairly straightforward: all bits are shifted left and 0-bits shift in from the right.
Example:   
 int  i = 2; // In binary, i is: 00000010
 i = i << 1; // Shift left by 1 bit:  00000100 <- 0

The ‘>>’ operator is slightly more complicated in that its behavior depends upon whether we are operating on a signed or unsigned integer. For unsigned integers, the right-shift is logical, i.e., the uppermost bits are filled with 0’s

  Example:  
  int  i = 128; // In binary, i is: 10000000

  i = i >> 1; // Shift right by 1 bit:       0->01000000   

(vi)Increment operator:


 The increment operator ‘++’ and decrement operator ‘--’ are shorthand ways  of adding or subtracting 1 from an integer-valued variable. Both operators are unary operators. The increment and decrement operators can each be utilized in two different ways, depending on whether the operator is written before or after the operand. They are prefix and postfix form. If the operator precedes the operand, then the value of operand will be altered before it is used for its intended purpose within the program. If, however the operator follows the operand then the value of the operand will be changed after it is used.

(Pre-increment) :++a equivalent to  a = a+1; returns a after increment

(Post-increment) :a++  equivalent to  a = a+1; returns a before increment

(Pre-decrement) : - -a equivalent to  a = a-1; returns a after decrement

(Post-decrement) : a- - equivalent to  a = a-1; returns a before decrement

The difference lies in what the value of the expression is (yes, the statement ‘i++’ is in fact an xpression).

Example: int i,j; i=3;
        j = i++; // j=3, i=4
Note that the value of the expression ‘i++’ is the value of i before incrementing.

Now consider:

Example:  int i,j; i=3;
     j = ++i; // j=4, i=4
With the prefix form of the operator, the value of the expression ‘++i’ is the value of i after incrementing.

(vii) Conditional operators: 


 The conditional operator ‘?:’ is like an embedded if...else statement. It is a Ternary Operator.   The main difference is a if…else statement cannot be used in an expression, the conditional operator can be used in the expressions.

Syntax 

expression 1 ? expression 2 : expression 3; (or) expression ? true_clause : false_clause;

First, the EXPR expression is evaluated. If true, the value of the conditional operator is the result of executing the TRUE_CLAUSE. Otherwise, the value of the conditional operator is the result of executing the FALSE_CLAUSE.

Example 1: ( i< 1) ? 0:200;  i is integer variable here.
The expression (i<1) is evaluated first, if it is true the entire conditional expression takes on the value 0. Otherwise, the entire conditional expression takes on the value 200.
Example 2: (x>y)  ? z=x : z=y ;  is equivalent to    if (x > y)  z = x;   else z = y;

(viii)  Special operators: 

(a) The Comma (,) Operator:  


 A strange operator, the comma ‘,’ is used to combine related statements/ expressions /any components as specified in the following case the comma operator can be used to separate variables names, if they are belonging to same data type and in input and output statements.
Example:     
 int a, b;
scanf (“ %d%d%f ”, &x, &y, &avg);

(b)The sizeof operator:


 This operator returns the size of its operand, in bytes(memory allocated). This operator always precedes its operand. The operand may be an expression, or it may be a cast. It is a unary operator.
Example:  
sizeof (x);   
sizeof (y);
If x is of integer type variable and y is of floating point variable then the result in bytes is 2 for integer type and 4 for floating point variable.

( c ) The  Cast operator:


  A cast is also considered to be unary operators. In general terms, a reference to the cast operator is written as (type). It converts the a variable value to the specified data type. It  is a unary operator.  

Example:  x=( int ) average;

(d) Addressing/Pointer  operators : 


        The C language defines pointers, and a set of operators which can be applied to them, or which allows them to be built. All these operators are unary operators. And they are
*ptr  returns the value pointed by the pointer ptr
&var  returns the address of variable var

 (e) Member Selection Operators: 


        There are two member selection operators dot(.) and arrow(à) operators. Specifically dot(.) operator used with structures, where as  arrow(à) operator is used with structures and pointers.




More:







.