Saturday, 10 September 2016

Operators in C || precedence and associativity of c operators

Operator : An operator is a symbol that tells the computer to perform certain mathematical (or) logical manipulations, Operators are used in programs to manipulate data and variables. The data items that operators act upon are called operands.
Some operators require two operands, while others act up on only one operand. The operators are classified into unary, binary and ternary depending on whether they operate on one, two (or) there operands respectively.

These operators can be categorized into the following major groups.
  1. Arithmetic operators
  2. Relational operators
  3. Logical operators
  4. Equality operators
  5. Assignment operators
  6. Increment and decrement operators
  7. Conditional operators
  8. Bitwise operators
  9. Special operators

1.Arithmetic operators

C provides all the basic arithmetic operators. They are shown in below table. The operators +, -,* and / all work the same way as they do in other languages. These can operate on any built- in data type allowed in C

Arithmetic operations.

i)Integer Arithmetic:

when both the operands in a single arithmetic expression such as a+b are integers, the expression is called an integer expression and the operation is called integer arithmetic. Integer arithmetic always yields an integer value.

In the below examples, if a and b are integers, then for a=14 and b= 4 we have the following results:
  1. A-b =10
  2. A+b= 14
  3. A*b= 56
  4. a/b= 3 (decimal part truncated
  5. a%b = 2( remainder of division)

Real Arithmetic : An arithmetic operation involving only real operands is called real arithmetic, Areal operand may assume values either in decimal(or) exponential notation

If x,y and z are floats, then we will have:
  1. X= 6.0/ 7.0 = 0.8572
  2. Y= 1.0/3.0 = 0.333
  3. Z = -2.0 /3.0 = -0.6667

The operator % cannot be used with real operands.

Mixed – Mode Arithmetic: when one of the operands is real and the other is integer, the expression is called a mixed mode arithmetic expression.

If either operand is of the real type, then only the real operation is performed and the results is always a real number. Thus 15/10.0 = 1.5 where as 15/10 = 1.

2.Relational operators

Relational operators are symbols that are used to test the relationship between two variables (or) between and a constant. We often compare two quantities, and depending on their relation takes certain decisions. These comparisons can be with the help of relational operators.

3. Logical operators

Logical operators are symbols that are used to combine (or) negate expressions containing relational operators.

i)Logical AND

Logical AND operator is used to simultaneously evaluate two conditions or expressions with relational operators. If expressions on both the sides (left and right side) of the logical operator is true then the whole expression is true.
 The expression to the left is (ac). The whole expression is true only if both expressions are true only if both expressions are true. If b is greater than a and c.

ii) Logical Or

Logical OR operator is used to simultaneously evaluate two conditions or expressions with relational operators. If one or both the expression on the left side and right side of the logical operator is true then the whole expression is true. The truth table of logical OR operator is given in table.
Truth table of logical OR (table)
For example (ac)
The expression to the left is (ac ). The whole expression is true if either b is greater than a or b is greater than c.

iii) Logical NOT

The logical NOT operator takes a single expression and negates the value of the expression . that is, logical NoT produces a zero if the expression evaluates to a non-zero value and produces al if the expression. The expression.
For example, int a=10, b;
B = !a;
Now the value of b=0. This is because b value of a=10. !a=0. The value of !a is assigned to b, hence, the result.

4. Equality operators

C language supports two kinds of equality operators to compare their operands for strict equality operators have lower precedence than the relational operators.

5.Assignment operators

Assignment operators are used to assign the result of an expression to a variable, The general format of assignment statement is:

Variable – name expression;

Where expression may be as Simple as a constant (or) as complex as an expression. The operator ‘=’ is called assignment operator. The left of assignment operator must be a variable.

Ex : F=1.8 * C+ 32.0; i= 10; a=b+c;

The usual assignment operator, ‘=’ in addition’C’ has a set of ‘shorthadnd’ assignment operators of the form.

V OP = exp;

Where V is a variable, exp is an expression and OP is a C binary arithmetic operator. The operator
OP = is known as the shorthand assignment operator.
  1. The assignment statement V OP=exp;
  2. Is equivalent to V = V OP (exp)

The use of shorthand assignment operators has three advantages:

  1. What appears on the left hand side need not be repeated and there fore it becomes easier to write.
  2. The statement is more concise and easier to read.
  3. The statement is more efficient.

6. Increment and decrement operators

C allows two very useful operators not generally found in other languages. These are the increment and decrement operators:
++ and - -
The operator ++ adds 1 to the operand, While – subtracts 1. Both are unary operators and takes the following form:
++m; or m++;
--m ; or m - -;
++m; is equivalent to m=m+1; (or m+=1;)
--m; is equivalent to m=m-1; (or m-=1;)
While ++m and m++ mean the same thing when they form statements independently, they behave differently when they are used in expressions on the right hand side of an assignment statement.

Consider the following:
m = 5;
y = ++m;
In the case, the value of y and m would be 6. Suppose, if we rewrite the above statement as
m = 5;
y = m++;
then, the value of y would be 5 and m would be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on left. On the hand, a postfix operator first assigns the value to the variable on left and then increments the operand.

7. Conditional operator

A ternary operator pair “?:” is available in C to construct conditional expressions of the form.
Exp 1 ? exp2 : exp 3
Where exp1, exp2, and exp3 are expressions.

 The operator? : works as follows : exp1 is evaluated first. If it is nonzero (true), then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false, exp 3 is evaluated and its value becomes the value of the expression. Note that only one of the expressions (either exp2 or exp3) is evaluated. For example, Consider the following statements.
a = 10;
b = 15 ;
x = (a>b)? a : b;
In this example, x will be assigned the value of b.

8. Bit wise operators

C has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. These operators are used for testing the bits, or Lists the bitwise operators and their meanings.

9. Special operators

These are used for special purpose in C language these operators are used in pointers, structures and unions etc. Some types special operators as
I ) unary operator
II) Comma operator
III) size of operator
IV) member selection operator

I) Unary Operator: Those that operators on a single operand. The unary operators are ++, - -, (underscore),& (address operand,* (indirection operator), size of operator and so on.

II) Comma Operator: When a no. of statement occurred in a C program having a relationship between expressions. Then we can write all expression are statements in a single expression using comma(,) operator. The comma operator are also used in variable declaration statement.

III) Size Operator: It is to display no.of bytes covered by a variable or an expression.
Ex: int a,b ;
B=size of (a); b contains 2

Iv ) Member selection Operator: These operators are used in structures, unions and pointers.
These are ‘.’ And ‘->’.

Operators precedence : In an expression, if it contains more than one operator, then which operator is evaluated first indicates its precedence or priority.

Operators associativity: The operators of the same precedence are evaluated either from ‘left to right’ or from ‘right to left’ depending on level. This is known as the associativy property of an operator


Post a Comment


Find Us On Facebook

C Programming


C++ Tutorial


Java Tutorial


Computer Basics


MS Office


Database Management