Operator
Usage
operands
|
Various types of Operators
C supports a rich set of operators. C operators can classified as
Review Questions
|
Use of Arithmetic Operators:
- Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.
- C provides all the basic arithmetic operators.
- They can operate on any built-in numeric data type of C.
- They cannot be used as Boolean type.
- They can use them on char types, since the char type in C is, essentially, a subset of int.
Representation of Operands on Arithmetic operators
- The operands acted upon by arithmetic operators must represent numeric values.
- Thus, the operands can be integer quantities, floating-point quantities or characters (the character constants represent integer values, as determined by the computer’s character set.
- If one or both operands represent negative values then the addition, subtraction, multiplication and division operations will result in values whose signs are determined by the usual rules of algebra.
Arithmetic operators can be classified as
Unary Arithmetic: It requires only one operand. Example: ++x -y Binary Arithmetic: It requires two operands. Example: a+b a-b a*b a/b a%b Integer Arithmetic: It requires both operands are integer values for arithmetic operation. Example: a=10, b=5 a+b=15 a-b=5 a*b=50 a/b=2 a%b=0 Floating-point Arithmetic: It requires both operands are float type for arithmetic operation. Example: a=6.5, b=3.5 a+b=10.0 a-b=3.0 a*b=22.75 Review Questions
|
About Division Operator (/): -
About Remainder Operator (Or) Modulus Operator(%): -
Example: 11 % 3 = 3: -11 % 3 = -2: 11 % -3 = 2: -11 % -3 = -2 The % operator is sometimes referred to as the modulus operator. Precedence
Write a program to perform arithmetic operations?
#include <stdio.h>
#include <conio.h> main() { int a,b; clrscr(); printf("Enter the values for a and b: "); scanf("%d %d", &a, &b); printf("After Addition: %d",a+b); printf("After Subtraction: %d",a-b); printf("After Multiplication: %d",a*b); printf("After Division: %d",a/b); printf("After (Modulo) Division : %d",a%b); getch(); } Output: Enter the values for a and b: 14 4 After Addition: 18 After Subtraction: 10 After Multiplication: 56 After Division: 3 After (Modulo) Division: 2 |
Difference between Division Operator(/) and Modulus Operator
1. The interpretation of the remainder operation is clear
when one of the operands is negative. 2. If a division operation is carried out with two floating-point numbers or with one floating-point number and one integer, the result will be a floating-point quotient. 3. The use of the division operation is determined by the usual rules of algebra. Example: - 7 / 2 = 3 |
1. The interpretation of the remainder operation is unclear when one of the operands is negative.
2. The remainder operator (%) requires that both operands be integers only. 3. Beginning programmers should exercise care in the use of the remainder operation. Example:- 7 % 2 = 1 |
Precedence and Associativity of Arithmetic Operators
Every operator in ‘C’ has a precedence associated
with it. The precedence is used to
determine how an expression involving more than one operator is evaluated.
The higher levels of precedence are evaluated first. The operators of the same precedence either from left to right or right to left depending on the level is known as associativity. There are two types of associativity namely 1. Left-to-Right associativity 2. Right-to-Left associativity
There are two distinct precedence or priority levels of arithmetic operators in C High priority * / % Low priority + - The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations. Review Questions
|
Basic evaluation procedure of Arithmetic operators
The basic evaluation procedure includes two left-to-right passes through the expression.
Evaluate an arithmetic expression without parentheses An arithmetic expression without any parentheses will be evaluated from left to right using the rules of precedence of operations. Example: - x = a – b / 3 + c * 2 - 1 where a=9, b=12, and c=3 the statement becomes x = 9 – 12 / 3 + 3 * 2 -1 During the first pass Step 1: x = 9 – 4 + 3 * 2 – 1 (12 / 3 evaluated) Step 2: x = 9 – 4 + 6 – 1 (3 * 2 evaluated) During the second pass Step 3: x = 5 + 6 – 1 (9 - 4 evaluated) Step 4: x = 11 – 1 (6 +5 evaluated) Step 5: x = 10 (11-1 evaluated) Evaluate an arithmetic expression with parentheses The order of evaluation can be changed by introducing parenthesis into an expression. Consider the same expression with parentheses, x = a – b / (3 + c) * (2 – 1) Where a=9, b=12, and c=3 the statement becomes x = 9 – 12 / (3 + 3) * (2 -1) During the first pass Step 1: x = 9 – 12 / 6 * (2 – 1) (3 + 3 evaluated) Step 2: x = 9 – 12 / 6 * 1 (2 – 1 evaluated) During the second pass Step 3: x = 9 – 2 * 1 12 / 6 evaluated Step 4: x = 9 – 2 2 * 1 evaluated Third pass Step 5: x = 7 Different operators with same priority When an expression contains operators of equal priority the tie between them is settled using the associativity of the operators. Consider the following expression, a = 3 / 2 * 5; here, there is a tie between operators of same priority, that is between / and *. This tie is settled using the associativity of / and *. But both enjoy the Left-to-Right associativity. Table shows for each operator which operator is unambiguous & which is not.
Since both / and * have Left-to-Right associativity and only / has unambiguous left operand (necessary condition for L to R associativity) it is performed earlier. That is, a=3/2*5; a=1*5; a=1; Same operators with same priority
When an expression contains same operators of equal priority and same associativity the tie between them is settled using the associativity of the operators. Consider the following expression, a = b = 3; here, this tie is settled using the associativity of = operators. But both assignment operators enjoy the Right-to-Left associativity. Table shows for each operator which operator is unambiguous and which is not. Since both = have Right-to-Left associativity and only the second = has unambiguous right operand (necessary condition for R to L associativity) it is performed earlier.
That is, a=b=3; b=3; a=b; |
For example, compare the age of two persons or the price of two items and so on. These comparisons can be done with the help of relational operators. All the relational operators have equal precedence. These operators are used to form Logical expressions. When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the results compared. That is, arithmetic operators have a higher priority over relational operators. Example: a >= 10 * (a+b) Equality Operators
|
Relational operations using Relational operators #include <stdio.h>
#include <conio.h> main() { int x=15,y=20; clrscr(); printf("The value of x>y is %d",x>y); printf("The value of x<y is %d",x<y); printf("The value of x>=y is %d",x>=y); printf("The value of x<=y is %d",x<=y); printf("The value of x = =y is %d",x = = y); printf("The value of x!=y is %d",x!=y); getch(); } Output: The value of x>y is 0 The value of x<y is 1 The value of x>=y is 0 The value of x<=y is 1 The value of x= =y is 0 The value of x! =y is 1 |
Definition: Operators which are used to combine two or more relational expressions are called as Logical operators.
Return They return either a TRUE (1) or a FALSE (0). C allows usage of three logical operators, namely && (AND), || (OR) and !(NOT). The operator && is composed of double symbols. Use: These operators are used to test more than one condition at a time. Why need to use Logical Operators? The following problems arises in the time of writing nested if-else statement 1. As the number of conditions go on increasing the level of indentation also goes on increasing. As a result, the whole program creeps to the right. 2. Care needs to match the corresponding ifs with their corresponding elses. 3 .Care needs to match the corresponding opening and closing braces. |
Lazy Evaluation
Logical operators employ a technique called as lazy evaluation.
They evaluate their left hand operand first, and then only evaluate the right hand operand only if it is required. Clearly FALSE && any operand(s) is always false in such case, the second and subsequent operands are not evaluated. Review Questions
1.What are Logical operators?
2.What is the use of Logical operators? 3.Why need to use Logical Operators? 4.Define Lazy Evaluation. |
Logical AND Expressions
If ((Hungry = =’Y’) && (Cash = = ‘Y’))
printf ("Can Eat"); If ((Hungry = =’Y’) && (Cash = = ‘N’)) printf ("Can not Eat"); If ((Hungry = =’N’) && (Cash = = ‘Y’)) printf ("Can not Eat"); If ((Hungry = =’N’) && (Cash = = ‘N’)) printf ("Can not Eat"); |
An expression of this kind which combines two or more relational expressions is known as a Logical expression or a Compound expression. Logical operators employ a technique called as lazy evaluation. They evaluate their left hand operand first, and then only evaluate the right hand operand only if it is required. Clearly FALSE && any operand(s) is always false in such case, the second and subsequent operands are not evaluated.
C allows usage of three logical operators, namely && (AND), || (OR) and !(NOT). The operators && and || are composed of double symbols. These operators are used to combine and also test two or more conditions. The following problems arises in the time of writing nested if-else statement |
Review Questions?
1.Name the Logical operators.
2.Define Logical Expression. 3.Define Compound Expression. 4.What are the problems arises in the time of writing nested-if st? |
1.As the number of conditions go on increasing the level of indentation also goes on increasing. As a result, the whole program creeps to the right. 2.Care needs to match the corresponding ifs with their corresponding elses. 3 .Care needs to match the corresponding opening and closing braces. |
………………………………………
If (percentage>=60) printf("First division"); else { if (percentage>=50) printf("Second division"); else { if (percentage>=40) printf("Third division"); else printf("Fail"); } } ………………………………………………..................... |
Logical operators are used to eliminate all the problems.See the following programming code,
-------------------------------------------------------
------------------------------------------------------- If (percentage>=60) printf("First division"); if ((percentage>=50) && (percentage<60)) printf("Second division"); if ((percentage>=40) && percentage<50)) printf("Third division"); if (percentage<40) printf("Fail"); ................................................................. |
Logical OR (||)
Logical OR Expressions
If ((CreditCard = =’Y’) || (Cash = = ‘Y’))
printf ("Can Buy"); If ((CreditCard = =’N’) || (Cash = = ‘y’)) printf ("Can Buy"); If ((CreditCard = =’Y’) || (Cash = = ‘N’)) printf ("Can Buy"); If ((CreditCard = =’N’) || (Cash = = ‘N’)) printf ("Can not Buy"); |
Lazy Evaluation
Logical operators employ a technique called as lazy evaluation. They evaluate their left hand operand first, and then only evaluate the right hand operand only if it is required. Clearly TRUE || any operand(s) is always TRUE in such case, the second and subsequent operands are not evaluated.
|
Logical NOT(!)
|
Write a program to check whether the given number is Positive or Negative using Logical Not operator.
#include <stdio.h>
#include <conio.h> main() { int a; clrscr(); printf("Enter n: "); scanf("%d", &n); if (!(n>0)) printf("Negative."); else printf("Positive."); getch(); } Output: Enter n: 10 Positive Enter n: -10 Negative |
Part-A
1. Define Operator.
2. What is the use of an Operator? 3. Define Operand. 4. List down the various types operators available in C. 5. List down the Arithmetic operators available in C. 6. List down the Binary operators available in C. 7. List down the Relational operators available in C. 8. List down the Equality operators available in C. 9. List down the Logical operators available in C. 10.List down the Unary operators available in C. 11.List down the Conditional operators available in C. 12.List down the Ternary operators available in C. 13.List down the Increment and Decrement operators in C. 14.List down the Arithmetic assignment operators in C. 15.List down the Shorthand assignment operators in C. 16.List down the Compound assignment operators in C. 17.List down the Bitwise operators available in C. 18.List down the Special operators available in C. |
Part-B
1. Write short notes on: Arithmetic Operators.
2. Write short notes on: Relational Operators. 3. Write short notes on: Logical Operators. 4. Write short notes on: Binary Operators. 5. Write short notes on: Equality Operators. 6. Write short notes on: Unary Operators. 7. Write short notes on: Conditional Operators. 8. Write short notes on: Ternary Operators. 9. Write short notes on: Increment Operators. 10. Write short notes on: Decrement Operators. 11. Write short notes on: Assignment Operators Oprs. 12. Write short notes on: Arithmetic Assignment Oprs. 13. Write short notes on: Shorthand Assignment Oprs. 14. Write short notes on: Compound Assignment Oprs 15. Write short notes on: Bitwise Operators. 16. Write short notes on: Special Operators. |