C-Tutorial: OPERATORS

Latest

Showing posts with label OPERATORS. Show all posts
Showing posts with label OPERATORS. Show all posts

Saturday, 19 November 2016

OPERATOR PRECEDENCE & ASSOCIATIVITY

Saturday, November 19, 2016 0
OPERATOR PRECEDENCE & ASSOCIATIVITY


In programming languages, expressions are evaluated based on precedence and associativity of the operators.

Precedence: If more than one operator is available in a single statement, order of evaluation depends on precedence of operators.  Precedence refers to rank of operators that follow order of evaluation.

Compiler follows order of evaluation as:

Highest precedence operator is evaluated first before the lowest precedence operator.
If two or more operators have same precedence, it follows associativity.

Example: X = 2;
Y = X + 10 * 2;

=   +   *

Y = X + 20

=   +

Y = 22


Associativity: Associativity refers to the direction of evaluations as from left to right or from right to left.
Left-to-Right associativity evaluates the expression by starting on the left and moving to the right.

Example: 3 * 8 / 4 % 4 * 5

* / % * L → R

24 / 4 % 4 * 5

/ % * L → R

6 % 4 * 5

% * L → R

2 * 5

*
10

Right-to-Left associativity evaluates the expression by starting on the right and moving to the left.

Example: Assume a = 8, b = 5 and c = 8

a += b *= c -= 5
+=    *=    -= R → L

a += b *= 3
*=    -= R → L

a += 15
+=

a = 23


Operator precedence & Associatity Table:

RANK
OPERATOR
ASSOCIATIVITY

1
( ) [ ] -> . ++ (POSTFIX)
- - (POSTFIX)
L to R
2
++ (PREFIX) - - (PREFIX) ! ~ sizeof unary minus &(Address) *(Pointer)
R to L
3
 */ %
L to R
4
+ -
L to R
5
<<       >>
L to R
6
<   <=    >    >=
L to R
7
== !=
L to R
8
&
L to R
9
^
L to R
10
|
L to R
11
&&
L to R
12
||
L to R
13
?:
R to L
14
= += -= *= /= %= >>= <<= &= ^= |=
R to L
15
, (comma operator)
L to R

Solve the following expressions

1. Z = ++X + Y-- - ++Y - X-- - X-- - ++Y - X—
Where X = 7 and Y = -3

Solution: X = 5, Y = -2 and Z = -15

2. Z = X++ + ++Y - X-- + --Y
Where X = 7 and Y = 9

Solution: X = 7, Y = 9 and Z = 18

/* EXAMPLE PROGRAM */

#include<stdio.h>
#include<conio.h>
main()
{
int a=9,b=12,c=3,x;
clrscr();
x=a-b/3+c*2-1;
printf("\n X=%d",x);
getch();
}

Read More

Special Operators in C

Saturday, November 19, 2016 0
Special Operators

C language supports some special operators such as unary operator, comma operator, sizeof operator, pointer operators (& and *) and member selection operators (. and ->).


a) Unary minus operator: Unary minus (-) operator changes sign of the given operand.  i.e., +ve sign is changed as –ve sign and –ve sign is changed as +ve sign.

Example: +10 → -10
-456 → +456

b) Comma Operator: Comma (,) operator is used to separate the operands from one to another.
Example: int x,y;

c) sizeof Operator: sizeof operator is a compile time operator used to return the number of bytes occupied by the given operand.  The general format of sizeof operator is:

Syntax: sizeof(Operand);
Here,
The operand may be a variable, a constant or a data type.

/* EXAMPLE PROGRAM FOR SIZEOF OPERATOR */

#include<stdio.h>
#include<conio.h>
main()
{
int i;
char ch;
float f;
double d;
clrscr();
printf("\n THE SIZEOF CH VARIABLE :%d BYTES",sizeof(ch));
printf("\n THE SIZEOF I VARIABLE :%d BYTES",sizeof(i));
printf("\n THE SIZEOF F VARIABLE :%d BYTES",sizeof(f));
printf("\n THE SIZEOF D VARIABLE :%d BYTES",sizeof(d));
printf("\n THE SIZEOF CHAR DATA TYPE :%d BYTES",sizeof(char));
printf("\n THE SIZEOF INT DATA TYPE:%d BYTES",sizeof(int));
printf("\n THE SIZEOF FLOAT DATA TYPE :%d BYTES",sizeof(float));
printf("\n THE SIZEOF DOUBLE DATA TYPE :%d BYTES",sizeof(double));
printf("\n THE SIZEOF CHAR CONSTANT VALUE :%d BYTES",sizeof('@'));
printf("\n THE SIZEOF INT CONSTANT VALUE :%d BYTES",sizeof(225));
printf("\n THE SIZEOF FLOAT CONSTANT VALUE:%d BYTES",sizeof(23.45f));
printf("\n THE SIZEOF DOUBLE CONSTANT VALUE :%d BYTES",sizeof(456.678));
}
Read More

Bitwise Operators in C

Saturday, November 19, 2016 0
Bitwise Operators


            
C language supports bitwise operators as &, |, ^, ~, << and >> operators.  These operators are used to manipulate data of the operands at bit level i.e., operations are performed on corresponding bits of the given operands.

Bitwise operators can operate only on integer quantities such as int, char, short int, long int etc.,

Bit-Wise AND, Bit-Wise OR and Bit-Wise Exclusive OR follows the following bit comparison tables.

Bit1
Bit2
Bit1 & Bit2
Bit1 | Bit2
Bit1 ^ Bit2

0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0


Here,
Ø  Bit-Wise AND compares the corresponding bits of the operands and produces 1 when both bits are 1; 0 otherwise.
Ø  Bit-Wise OR compares the corresponding bits of the operands and produces 0 when both bits are 0; 1 otherwise.
Ø  Bit-Wise Exclusive OR compares the corresponding bits of the operands and produces 0 when both bits are same; 1 otherwise.

For the above operations consider the following number conversion system for octal and hexa-decimal numbers.

NUMBER
OCTAL
HEXADECIMAL

0
000
0000
1
001
0001
2
010
0010
3
011
0011
4
100
0100
5
101
0101
6
110
0110
7
111
0111
8

1000
9

1001
A - 10

1010
B – 11

1011
C – 12

1100
D – 13

1101
E – 14

1110
F - 15

1111

Example:
           
1.         X         : 011                0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
            Y         : 027                0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1             (Octal)

X&Y   :                       0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1             : 1
X|Y     :                       0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1             : 37
            X^Y    :                       0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0             : 36

2.         X         : 0X7B                        0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1
            Y         : 0X129           0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1     (Hexadecimal)

            X&Y   :                       0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1             : 29
            X|Y     :                       0 0 0 0 0 0 0 1 0 1 1 1 1 0 1 1             : 17B
            X^Y    :                       0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0             : 152


One’s Complement (or) Bit Negation operator is a unary operator that complements the bits of the given operand. i.e., Bit 0 converted into Bit 1 and Bit 1 converted into Bit 0.

Example:

X         : 0X7B                        0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1
~X       :                       1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0             :           FF84


Shift Operators:                    Left shift operator (<<) and right shift operator (>>) are known as shift operators.  They perform left and right shifts of their operand bits by the number of bit positions specified by the argument which must be integer and positive number.

Left Shift Operator Syntax:

            VariableName << NoOfBitPositions;

Example:        Let X = 36
                        X << 2;

Ø  While performing left shift operations, there is a loss of data at MSB side.
Ø  The vacated positions at LSB side are filled with zeros.
Ø  For each shift value by the number of bits, the result value is equivalent to multiplication by 2.

Right Shift Operator Syntax:

            VariableName >> NoOfBitPositions;

Example:        Let X = 36
                        X >> 2;                          

Ø  While performing right shift operations, there is a loss of data at LSB side.
Ø  The vacated positions at MSB side are filled with zeros.
Ø  For each shift value by the number of bits, the result value is equivalent to division by 2.

/* EXAMPLE PROGRAM FOR SHIFT OPERATORS */

#include<stdio.h>
#include<conio.h>

main()
{
            int x;
            clrscr();
            x=36;
            printf("\nLeft Shift Result:%d",x<<2);
            x=36;
            printf("\nRight Shift Result:%d",x>>2);

}
Read More