3.5 Operators in C

Operators in C
Related Questions:
What is meant by the operator in C?
What are the Bitwise Operators?
What is an operator describe different types of operators?
What are the operators used in C language?
What do you mean by the ternary operator in C?

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators.

  • Arithmetic Operator
  • Relational Operator
  • Logical Operator
  • Bitwise Operator
  • Assignment Operator
  • Misc Operator
3.5.1 Arithmetic Operator

The basic operators for performing arithmetic are the same in many computer languages:
  1.    +                      Addition
  2.    -                       Subtraction
  3.    *                      Multiplication
  4.    /                       Division
  5.    %                     Modulus (Remainder) 

 For exponentiations, we use the library function pow. The order of precedence of these operators is % / * + - . It can be overruled by parenthesis.

Division of an integer quantity by another is referred to integer division. This operation results in truncation. i.e.When applied to integers, the division operator/discards any remainder, so 1 / 2 is 0 and 7 / 4 is 1. But when either operand is a floating point quantity (type oat or double ), the division operator yields a floating point result, with a potentially non zero fractional part.So 1 / 2.0 is 0.5, and 7.0 / 4.0 is 1.75.


An operator acts upon a single operand to produce a new value is called a unary operator. The decrement and increment operators ++ and -- are unary operators. They increase and decrease the value by 1. 
sizeof() is another unary operator. The output given by the sizeof() operator depends on the computer architecture. The values stated by the sizeof() operator down below are based on a 16-bit compiler. 
  1. int x, y;
  2. y=sizeof(x);
The value of y is 2. The sizeof() of an integer type data is 2, that of a float is 4, that of double is 8, that of char is 1.

3.5.2 Relational Operator

< ( less than ),
<= (less than or equal to ),
> (greater than ),
>= ( greater than or equal to ),
== ( equal to ) and
! = (not equal to ) are relational operators.

A logical expression is an expression connected with a relational operator.

For example: `b*b - 4*a*c<0 is a logical expression. Its value is either true or false.

  1. int i , j , k ;
  2. i =1;
  3. j =2;
  4. K=i+j ;

    The expression k>5 evaluates to false and the expression k<5 evaluates to true.
3.5.3 Logical Operator

The relational operators work with arbitrary numbers and generate true/false values.You can also combine true/false values by using the Boolean operators, which take true/false values as operands and compute new true/false values. The three Boolean operators are: 
Examples:
1.  && AND 15 
2.   | | OR 
3.   ! NOT 

The && ("and'') operator takes two true/false values and produces a true (1) result if both operands are true (that is, if the left- hand side is true and the right-hand side is true). 

The || ("or'') operator takes two true/false values and produces a true (1) result if either operand is true.
The ! ("not'') operator takes a single true/false value and negates it, turning false to true and true to false (0 to 1 and 1 to 0). 

&& (and ) and | | (or) are logical operators which are used to connect logical expressions. Where as ! ( not) is a unary operator, acts on a single logical expression. 
Examples:
1. ( a<5) && ( a>2) 
2. ( a<=3) j j ( a>2) 

In the examples, if a= 3 or a=6 the logical expression returns true. 

3.5.4 Assignment Operator

These operators are used for assigning a value of expression to another identifier. 
=, + =, - = , * =, /= and % = are assignment operators. 
Examples
a = b+c    results in storing the value of 'b+c' in `a'. 
a += 5      results in increasing the value of 'a' by 5. 
a /= 3 results in storing the value a/3 in 'a' and it is equivalent a=a/3 

3.5.5 Conditional Operator

The operator ?: is the conditional operator. It is used as variable 1 = expression 1 ? expression 2 : expression 3. Here expression 1 is a logical expression and expression 2 and expression 3 are expressions having numerical values. If expression 1 is true, value of expression 2 is assigned to variable 1 and otherwise, expression 3 is assigned. 
Examples:
1. int a , b , c , d , e ;
2. a=3; b=5; c=8;
3. d=(a<b) ? a : b ;
4. e=(b>c ) ? b : c ; 

The evaluation of the expression results the value of d = 3 and e = 8. 

3.5.6 Bitwise Operator

C has a distinction of supporting special operator known as bitwise operator for manipulation of data at bit level. These operators are used for testing bits or shifting them right or left. Bitwise operator may not be applied to float or double. Following are the bitwise operators.
Examples: 
  1. & bitwise AND 
  2. | bitwise OR
  3. ^ bitwise exclusive OR 
  4. << shift left 
  5. >> shift right

No comments:

Post a Comment