Wednesday, April 2, 2008

Understanding Operators in C

Operators in C

C has a rich set of operators that makes C so powerful. Operators may be classified, for the purpose of understanding as follows:

1. Classify operators on the basis of Number of operands:-

Unary Operator which takes one operand

Binary Operator which takes two operands

Ternary Operator which takes three operands

E.g of Unary Operator -x

Here the Operator is unary negation i.e. -

and the Operand is the variable x .

Unary Operators are ! ( not operator.)

& ( address operator )

++ (increment operator ) ,

-- (decrement operator )

- (Unary negation)

* (Indirection or dereferencing operator)


E.g of Binary Operator a + b

Here the operator is addition +

and the operands are a and b

2. Classify operators on the basis of the operations they perform :-

The operators are classified as:-

-Arithmatic Operators ( + , - , * , / etc)

-Relational Operators (> , < , >= , <= , = = )

-Logical Operators (&& , || , ! )

-Assignment Operators ( = , *= , += , /= , %= , -=)

and so on

Precedence & Associativity of operators :-

It is very important to understand these two terms in C.

Precedence :- When there are several operators in one expression , which one will be carried out first? This is what precedence is all about .

Note :-

Unary Operators have a higher precedence than binary Operators.

Arithmetic operators have a higher precedence to relational operators.

Relational operators have a higher precedence to logical operators.

Within arithmetic operators / , * , % have a higher precedence to + , - .

/ , * , % have equal precedence.

Associativity: Will the operator scan the left side data first or the right side data ? Associativity is specified as:

Left -> Right (L -> R)

Right -> Left (R -> L)

1. The arithmetical addition operator + associates L -> R. This means it reads the data on the left side first and then the data on the right side.

2. The assignment operator = associates R -> L. This means it reads the data on the right side first and then assigns it to the variable on the right side.

Some discussion on some operators:

Arithmetic operators :

All are binary operators, associate L - > R except unary negation which associates R -> L .

* Multiplication Operator

/ Division Operator

% Modulus Operator returns the remainder of integral division

+ Addition

- Subtraction, Unary negation

Note :

% operator is applicable only to char and int types; cannot be applied to floating point numbers. The % operator returns a periodic value. x % n always returns a value between 0 to n-1 for any value of x . The % operator returns the sign of the numerator irrespective of the sign of denominator.

Program :

Accept an amount and rate of an item, outputs how many items can be purchased and what is the balance amt

main( )

{

int amt, rate;

clrscr( );

printf(“Enter amount : ”);

scanf(“%d” , &amt);

printf(“Enter rate : “);

scanf(“%d” , &rate);

printf(“No. of articles = %d\nBalance Rs. %d”, amt/rate,

amt % rate) ;

getch( );

return 0;

}

Relational Operators: Binary operators, associate L -> R

> (Greater than)

< (Less than)

>= (Greater than or equal to)

<= (Less than or equal to)

= = (Relational equality)

!= (Relational inequality)

Note:

Relational operators return 1 if true and 0 if false.

<, >, >=, <= have equal precedence

= = , != have equal precedence

<, >, >=, <= have higher precedence to = =, !=

Relational operators have a lower precedence to arithmetical operators.


Logical Operators :

&& logical and operator (binary, associates L -> R)

|| logical or operator (binary, associates L -> R)

! logical not operator (unary, associates R -> L)

Truth table for logical operators: ( T : true F: false)

! operator

x

!x

T

F

F

T

|| operator

x

y

X || y

T

T

T

T

F

T

F

T

T

F

F

F

&& operator

x

y

X && y

T

T

T

T

F

F

F

T

F

F

F

F

Note:

Logical operators return 1 if true and 0 if false.

Any expression evaluating to a non zero value is treated as true

Any expression evaluating to a value of zero is treated as false

The logical not operator ( ! ) has the highest precedence

&& operator has higher precedence to || operator

Both && and || operator have lower precedence to relational operators.

Program:

Accept a character from user. Check if it is in upper case or lower case

main( )

{

char ch ;

clrscr( );

printf(“Enter a character:- ”);

scanf(“%c” , &ch);

if( ch >= ‘A’ && ch <=’Z’)

printf(“%c is in upper case”, ch);

else

if( ch >= ‘a’ && ch <= ‘z’)

printf(“%c is in lower case” , ch );

else

printf(“%c is neither in lower case nor in upper case”);

getch( );

return 0;

}

Program 2:

main( )

{

clrscr( );

printf(“%d %d %d %d” , 10 > 20 , 20 ==20 , !( 50 != 100) , 20 && 23);

}

Output: 0 1 0 1

Explanation:

10 > 20 is false , hence 0, 20 == 20 is true hence 1

50 != 100 is true hence 1 ; not true is false; hence 0

20 && 23 is interpreted as true and true, which evaluates to 1


Increment and Decrement operators:- ++ , --

Both are unary operators, associating from R -> L , with highest priority.

The ++ operator increases the value of its operand by one, while the decrement operator decreases the value of its operand by one.

Program:

main( )

{

int a = 10 ;

a++;

printf(“a = %d” , a );

++a ;

printf(“\na = %d” , a );

}

Output: a = 11

a = 12

Post and pre increment/decrement

When the ++/-- operators are part of an expression, then there are two possibilities - post and pre increment/decrement.

x = ++a ;

This is interpreted as : first increase the value of variable a by one and then assign it to x

x = a++ ;

This is interpreted as : first assign the value of variable a to x, then increase the value of variable a by one before executing the next statement.

No comments: