Write about Operators in ‘C' Language?

C supports a rich set of built-in operators.

Operator: operator is a symbol that tells the computer to perform certain mathematical (or) logical manipulations. Operators are used in programs to manipulate data & variables.

If an operator acts on single operate then it is called unary operator. If an operator acts on two operands then it is called binary operator. If an operator acts on three operands then it is called ternary operator.

C operators can be classified into number of categories. They include:

1. Arithmetic operators
2. Relational operators
3.Logical operators
4 .Assignement operators
5. Increment & Decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operator

1. Arithmetic operators:

C provides all the basic arithmetic operators arithmetic operators are used to perform arithmetic calculations such as addition, subtraction, multiplication & division.

Operator
Meaning
+
Addition or unary plus
Subtraction or unary minus
X
Multiplication
/
Division
%
Modulo division

These can operate on any built in datatype allowed in ‘C’. 

Integer arithmetic :

When both the operands in a arithmetic exp are integers, the exp is called an integer exp & the operation is called integer arithmetic. Integer arithmetic always yields an integer value.

Real arithmetic:

An arithmetic operation involving only real operands is called real arithmetic.
 Ex: x = 10.0/3.0 Þ 3.333

Mixed mode arithmetic: 

When one of the operands is real & 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 & the result is always a real number.
  Ex: 15/10.0  Þ 1.5

2. Relational operators:

Relational operators are used to compare two quantities & depending on their relation, take certain decisions. C supports 6 relational operators .

Relational Operators
Operator
Meaning
< 
Is less than
< =
Is less than or equal to
> 
Is greater than
> =
Is greater than or equal to
= =
Is equal to
! =
Is not equal to

Ex: 10 < 20 is true  ® 1
       20 < 10 is false ® 0

The value of a relational expression is either 1 or 0 It is 1 if the specified relation is true & 0 If the relation  is false.

3.Logical operators:

C has three logical operators

&&
Logical AND
!!
Logical OR
!
Logical NOT
    
The logical operators && and !! are used when we want to test more than one condition and make decisions.

Ex:  a > b && x == 10

Which combines two or more relational expression is termed as a logical exp or a compound relational expression.

Logical expression also give a value of 1 or 0, according to the truth table. In the above expression it give true only if a > b is true & x = = 10 is true. If either of them are false, the expression Is false.

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

Ex:
operators

4.Assignment operator:

Assignment operator are used to assign the result of an expression to a variable.
 assignment operator is =.

In addition, C has a set of shorthand assignment operators of the form:

Syntax: 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.These are the combination of both assignment and arithmetic operators. The above syntax is equal to

V = V OP (exp);

Ex:  x + = 3

When this statemant is executed, 3 is added to the old value of x. if x is 5 it becomes 8.

Ex:  a = a % b                              a % = b
       a = a * (n + 1)                        a * = n + 1
       a = a/(n + 1)                           a / = n + 1

5. Increment & decrement Operator:

C allows two very useful operators not generally found in other languages. These are

Increment operator ++ and

Decrement operator --.

The operator ++ adds 1 to the operand and while -- subtracts 1. Both are unary Operators.

Syntax: ++ m; (or) m ++ ;

++ m; is equal to m = m + 1, (or m+ = 1;)

– – m; (or) m – –;

– – m; is equal to m = m – 1; (or m – = 1;)

We use the increment & decrement statements in for & while loops extensively.

when postfix ++(or – –) is used with a variable in an expression, the expression is evaluated first using the original value of the variable and then the variable is incremented (or decremented) by one.

When prefix ++(or– –) is used in an expression, the variable is incremented (or decremented) first & then expression is evaluated using the new value of the variable.

Ex:–                a = 5;                                                              a = 5
                        pf(“%d”, a);–5                                                pf(“%d”, a);–5
                        pf(“%d”, ++a);–6                                            pf(“%d”, – – a);–4
                        pf(“%d”, a++);–6                                            pf(“%d”, a– –):–4
                        pf(“%d”, a);–7                                                pf(“%d”, a);–3

6.Conditional operator:

Conditional operator use three operands to operate. That’s  why it is also called as ternary operator. It is denoted by ?:

Syntax:– exp1?exp2:exp3;

Where exp1, exp2& exp3: are expressions.

The operator ?: works as follows:

exp1 is evaluated first. If it is true, then the expression, exp2 is evaluated & becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value of the expression.

Note that only one of the expression (exp2 or exp3) is evaluated.

Ex:– a = 10;
         b =15;
         x = (a>b)?a:b;

In this example, x will be assigned the value of b. 

7. Bitwise operator:

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 shifting then right or left.

Bit wise operators may not be applied to flood or double.

Operator
Meaning
&
Bitwise AND
!
Bit wise OR
^
Bit wise exclusive OR
<< 
Shift left
>> 
Shift right

8.Special operators:–

C supports some special operators such as comma operator and sizeof operator.

Comma operator:

The comma operator can be used to link the related expressions together.

Ex:– value = (x = 10, y = 5, x+y);

First assigns the value 10 to x, then assigns the value 5 to y, and finally assigns 15 to value.

Exchanging values: t=x, x = y, y = t;

The sizeof operator:–

The sizeof operator is a compile time operator and when used with a operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a datatype qualifier.

Ex:–    m= sizeof (sum);  –variable
            n = sizeof (long int);   –datatype.
            k = sizeof (2352);  –constant.

The sizeof operator is normally used to determine the lengths of arrays & structures when their sizes are not known to the programmer.

It is also used to allocated memory space dynamically to variables during execution of a program.

Related

C Language 7269164791301049207

Post a Comment

emo-but-icon

item