Computer Concept & Programming In C - Unit II - 9

Q.10 What are expressions and operators? Discuss in detail.
Related Questions -
Q. Discuss about airthmetic operators and relational operators.                   (AKTU. 11 - 12)
Q. List various types of loical operators in C language.                        (AKTU. 2013-14)
Ans. Expressions and operators: - 
Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the expression will be manipulated.
C is very rich in operators and is sometimes called “the language of operators”. C Operators may be classified as -
Arithmetic operators, logical operators, relational operators, assignment operators, pointer operators, special operators, bit wise operators.
(1) Arithmetic Operators
      Operator Action
- Subtraction and unary minus
+ Addition
* Multiplication
/ Division
%         Module Division
- -        Decrement
+ +       Increment
The table above lists the C Arithmetic operators. We dealt before with the first four operators in the table (-, +, *, /) which are used for subtraction, addition, multiplication and division. The three new operators are:
(i) The modulo division  (ii) The increment  (iii) The decrement operator.
(i) Modulo Division Operator: -
The modulo division operator (%) gives the remainder of the integer division. The following example demonstrates the use of both the division and the modulo division operators in dividing 10 by 3.
# include <stdio.h>
# include <conio.h>
void main ( )
{
 int x = 10, y = 3;
 printf (“\ n Integer division 10/3 = %d”, x/y);
printf (“\ n Remainder of integer division 10%3 = %d”, 10%3);
      getch ( );
}
The output of the above program is:
Integer division 10/3 = 3
Remainder of integer division 10%3 = 1
(ii) Increment and decrement operators: -
The increment and the decrement operators are very useful operator. The operator “++” means “add 1” and the operator “- -” means “subtract 1”.
Instead of using the following expression to increment a counter:
a = a + 1; you may use the C expression ++a; also the expression: a = a – 1; is the same as the expression - - a;
The two operators + + and - - are useful with short cuts. For example, instead of writing the following two statements
b = b + 1;
sum = a + b; you can write the same logic in one step as
sum = a + (++b);
the increment and decrement operators may precede or follow the operand, which means that the following expressions are equivalent: x + +, + +x;. In some cases however, there is a difference, consider the two statements.
Sum 1 = a + + + b;
Sum 2 = a + b + +;
In the first statement the variable “b” is incremented and then added to “a” while in the second statement the addition takes place before the incrementing The following program puts these two statements into action.
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a = 8, b = 2, x = 8, y = 2, sum 1, sum 2;
printf(“\ n product of 8*2 = %d”, 8*2);
printf (“\ n Quotient of 8/2 = %d”, 8/2);
printf (“\ n sum of 8+2 = %d”, 8+2);
printf (“\ n difference of 8–2 = %d”,8-2);
sum 1 = a + (+ + b);
sum 2 = a + (b + +);
printf (“\ n sum 1 = % d”, sum 1);
printf (“\ n sum 2 = % d”, sum 2);
   getch ( );
}
The output of the preceding program will be
Product of 8 * 2 = 16
Quotient of 8 / 2 = 4
Sum of 8 + 2 = 10
Difference of 8 – 2 = 6
Sum 1 = 11
Sum 1 = 11
(2) Relational And Logical Operators: -
The relational operators are used to compare values forming relational expressions. The logical operators are used to connect relational expressions together using the rules of formal logic. Both types of expressions produce TRUE or FALSE results. In C, FALSE is the zero, while any non-zero number is TRUE. However the relational and logical operators produce the value “1” for the true and the value “0” for false. The following table shows relational and logical operators used in C. They are lower in precedence than arithmetical operators. This means that the expression: 100 > 90 + 9 is equivalent to the expression: 100 > (90 + 9). This is because in both cases the addition is evaluated first. However, you will probably feel better if you will use parenthesis.
Relational Operators: -
Operators       Action
> Greater than
> =         Greater than or equal
< Less than
< =         Less than or equal
= =         Equal
! =        Not equal
Logical operators: -
Operators Action
& &          AND
| | ¦ ¦ OR
! NOT
Note: -
Notice that the equality operator (= =) is defined in C than in any other language. Don’t confuse it with the assignment operator (=).
Evaluation of Logical Expression: -
To understand how a logical expression is evaluated consider the expression: ((x = = y) & & (w > = z)).
This expression says: “x is equal to y AND w is greater than or equal to z”. In other words the expression is evaluated as TRUE if both of the following conditions are met: (1) The expression x = = y is evaluated as TRUE, and (2) The expression w > = z is evaluated as TRUE. If you replace the AND (&&) by an OR (||) then the expression is evaluated as true if either one of the two expressions is evaluated as TRUE.