# Chapter 3

Operators

**Q1: What is an Expression? Expression:
**

An expression is used for calculating the value of a formula. It is formed by combining different operands and operators. Its evaluation gives a single value. The operands may be constant values, different variable names and functions. Parenthesis may also be used in an expression. For example, to calculate the volume of a box with sides A, B & C, the expression is written as: AxBXC Where A, B, C are variable names and are called operands. The multiplication sign 'x' is called operator. The combination of operands and operators makes an expression.

**Q2: What are Arithmetic Operators? Arithmetic Operators:
**

The arithmetic operators are the symbols that are used for arithmetic operations. These are used in arithmetic expressions. Each arithmetic operator operates upon one or two numeric values (constants or variables) and returns a single value. The following arithmetic operators are used in C.

Operator

+ * / %

Meaning

Unary Minus Add Subtract Multiply Divide Remainder

All arithmetic operators, except the remainder operator, are used for all types of numeric data. The remainder operator is also called modulus operator. It can only be used for integer type data. It returns the reminder when one integer is divided by another integer. For example, 5%3 returns the remainder 2.

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

**Q3: Distinguish between binary Operators and unary Operators? Binary Operators:
**

The operators that operate on two data items are called binary operators. For example, multiplication is a binary operator because it operates on two values and multiplies one value by another.

Unary Operators:

The operators that need only one data item to operate on are called unary operators. The unary minus (-) operator is an example of unary operator.

**Unary Minus (-) Operator
**

The unary minus (-) operator is used to reverse the sign of a value. It makes a positive value negative, and vice versa. For example: int a, b; a = 87; b = -a; In the third statement, the unary minus (-) operator reverses the sign of the values in variable a and assigns it to the variable b. the unary operators have higher precedence over binary operators.

**Q4: What is an arithmetic expression? Arithmetic Expression
**

An arithmetic expression is a combination of variables, constants and arithmetic operators. It is used to calculate the value of an arithmetic formula. It returns a single value after evaluating the expression. For example, the formula to find the perimeter of a rectangle whose sides are a and b is 2(a + b). This formula is written as: 2*(a+b)

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Formula 5/9 (F-32) a2+b2+2ab

Operators

Expression (F-32)*5/9 a*a + b*b + 2*a*b

Q5: Write the following formulas as C language expressions?

Program:

Write a program to perform the arithmetic operations by using all arithmetic operators. Also print the results on the screen. #include<stdio.h> #include<conio.h> main() int d, p, s, m, r; p = 5+2; s = 5-2; m = 5*2; d = 5/2; r = 5%2; printf ("Addition of 5 & 2 is =%d\n",p"); printf ("Subracting of 5 & 2 is =%d\n",s"); printf ("Multiplying of 5 & 2 is =%d\n",m"); printf ("Dividing of 5 & 2 is =%d\n",d"); printf ("Remainder of 5/2 is =%d\n",r"); getch(); } Output Addition of 5 & 2 is =7 Subracting of 5 & 2 is =3 Multiplying of 5 & 2 is =10 Dividing of 5 & 2 is =2 Remainder of 5/2 is =1

**Q6: What is an Assignment statement?
**

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Assignment Statement:

Operators

The statement that is used to assign a value to a variable is called an assignment statement. The assignment statement evaluates an expression and then assigns its value to a variable. The assignment operator "=" is used to assign the calculated value to a variable. The expression is written on the right side of the assignment operator. The variable to which the value of the expression is to be assigned is written on the left side of the assignment operator. The syntax of the assignment statement is given as: var = expression; var expression variable name to which the value of the expression after calculation is assigned. The variable type must be the same as the type of value returned by the expression. an expression may be a constant, a variable or a combination of operands (variables and constants) and arithmetic operators.

Program

Write a program to exchange the values of two variables using assignment statement and print the exchanged values on the screen. #include<stdio.h> #include<conio.h> main() { int a, b, c; a = 200; b = 100; c = a; a = b; b = c; printf ("value of a = %d\n", a); printf ("value of b = %d\n", b); getch(); }

Output

value of a = 100 value of b = 200

Program:

Write a program to assign the numeric value to a variable year. Calculate the number ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

of months, and print the result on the screen. #include<stdio.h> #include<conio.h> main() { int year, month; year = 20; month = year*12; printf ("Years = %d\n", year); printf ("Months = %d\n", month); getch(); }

Output

Years = 20 Months = 240

**Q7: Differentiate Between Lvalue and Rvalues? Lvalue and Rvalues:
**

The value that can only be written on the right side of the '=' is called Rvalues. Constants cannot be written on the left of '='. They can only be written on the right of '=' and are therefore Rvalues. Similarly, the value that can be written on the left side of '=' is Lvalue. Variables can be written on the left of '=' can be Lvalue.

**Q8: What is a compound Assignment Statement?
**

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

**Compound Assignment Statement:
**

The assignment statement can also be used to assign one value to many variables. This type of assignment statement is called compound assignment statement. For example, to assign an integer value 16 to two integer type variables x and y, the compound assignment statement is written as: x =y=16;

Program:

Write a program to assign a value 515 to integer type variables x, y, a, b and c. Also calculate the sum of the variables and print the result on the screen. #include<stdio.h> #include<conio.h> main () { int x, y, a, b, c; x = y = a = b = c = 515; s = x + y +a + b +c; printf ("Sum = %d\n", s); getch(); }

Output

Sum = 2575

**Q9: What is a compound assignment expression? Compound Assignment Expression:
**

Compound assignment expression is used to add, subtract, multiply or divide a value to or from a variable without writing the variable on either side of the assignment operator. In this assignment expression, the arithmetic operator is combined with the assignment operator. Its syntax is: var op = expression; var represents the variable to which the value is to be added, subtracted, multiplied or divided. op represents the arithmetic operator i.e. +, -, * and / etc. expression represents the arithmetic expression whose value is to be added, subtracted, multiplied or divided from the variable var. it may be an expression, a variable or a constant. For example, to add 10 to a variable xy that already has a value, the simple arithmetic statement is written as: xy = xy + 10; ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

The above statement can be written as compound assignment expression as: xy + =10; Following are some examples of compound assignment expressions: x +=9 same as x = x + 9; x -=2; same as x = x – 2; x *=7; same as x = x * 7; x /=3; same as x = x / 3;

Program:

Write a program in C to assign three values to three integer type variables a, b, c. add variables a & b and multiply their sum to variable c. Use compound assignment statement. #include<stdio.h> #include<conio.h> main () { int a, b, c; a = 3; b = 6; c = 9; c *= a + b; printf ("Result = %d\n", c); getch(); } OUTPUT Result = 81

**Q10: Distinguish between increment and decrement operators? Increment & Decrement Operators:
**

The operator that is used to add 1 to the value of a variable is called increment operator. Similarly, the operator that is used to subtract 1 from the value of a variable is called decrement operator. Only variables can be incremented or decremented. Constants and expressions cannot be incremented or decremented.

**Q11: Explain the increment operator. Distinguish between prefixed and postfixed increment operator?
**

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

**The Increment Operator (++):
**

The increment operator is represented by a double plus (++) sign. It is used to add 1 to the value of a variable. This operator can be used before or after the variable name. For example, to add 1 to the value of a variable x, the increment operator is written as: x++ or ++x The increment operator can be written either before or after the variable name. if it is written before the variable name, it is known as prefix operator. If it is written after the variable name, it is known as postfix operator. Prefix and postfix operators have different effects when they are used in expressions. Only variables can be incremented or decremented. Constants and expressions cannot be incremented or decremented. Thus the following are invalid expressions: 87++ Invalid, constants cannot be incremented or decremented ++ (b * b – 4*a*c) Invalid, expressions cannot be incremented or decremented.

**Prefix Increment Operator:
**

When an increment operator is used in prefix mode in an expression, it adds 1 to the value of the variable before the value of the variable is used in the expression. For example, b = ++a; is equivalent to a = a + 1; b = a;

Program

#include<conio.h> #include<stdio.h> main() { int a, b, c, sum; a = 1; b = 1; c = 3; sum = a + b + (++c(; printf ("sum = %d\n", c); printf ("c = %d", c); getch(); } Output Sum = 6 C=4 In the above program, 1 will be added to the value of c before it is used in the expression, thus after execution, the sum will be equal to 6 and value of c will be 4.

Postfix Increment Operator:

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

When an increment operator is used in postfix mode in an expression, it adds 1 to the value of the variable after the value of the variable has been used in the expression. For example, b= a++; Is equivalent to b= a; a= a + 1;

Q12: Explain the decrement operator. Distinguish between prefixed and postfixed decrement operator? The Decrement Operator (--):

The decrement operat0or is represented by a double minus (--) sign. It is used to subtract 1 from the value of a variable. For example, to subtract 1 from the value of a variable y, the decrement statement is written as: y--; or --y;

**Prefix Decrement Operator:
**

When a decrement operator is used in prefix mode in an expression, it subtracts 1 from the value of the variable before the value of the variable is used in the expression. For example: b = --a; is equivalent to a = a – 1; b = a;

Program

#include<stdio.h> #include<conio.h> main () { int a, b, c, sum; a = 1; b = 1; c = 3; sum = a + b + --c; printf ("Sum = %d\n", sum); printf ("c = %d", c); getch(); }

Output

Sum = 4 c=2 In the above program, 1 will be subtracted from the value of c before it is used in the expression. Thus after execution, the sum will be equal to 4 and value of c will be 2. ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

Postfix Decrement Operator: When a decrement operator is used in postfix mode in an expression, it subtracts 1 from the value of the variable after the value of the variable has been used in the expression. For example, b = a--; Is equivalent to b = a; a = a – 1;

Program

Write a program to initialize a value to a variable. Calculate the cube of the given value. Also use the comments in the program. #include<stdio.h> #include<conio.h> mian() { int a, c; a = 10; c = a*a*a; printf ("cube of %d = %d", a, c); getch (); }

Output

Cube of 10 = 1000

Q13: What is Order of Precedence of Operators? Explain the order of precedence of arithmetic Operators? Order of Precedence of operators:

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

The order in which the operators are evaluated is called the order of precedence of the operators. It is also known as hierarchy of operation. Each operator has a fixed order in which it is evaluated in relation of other operators. The operators with higher precedence order are evaluated before those with lower order. When an arithmetic expression is evaluated, the computer performs only one operation at a time, in an arithmetic expression in C, the arithmetic operators are evaluated in the following order: 1. All multiplications and divisions are performed first from left to right. 2. All additions and subtractions are then performed from left to right. 3. If parentheses are used in an expression, the expressions within parentheses are first computed from left to right. 4. When parentheses are used within parentheses, the expression within innermost parentheses is evaluated first. For example, (4-(3*5))+2 is evaluated as follows: 1. (3*5) is computed and returns value of 15. 2. 4-15 is computed and return value of -11. 3. -11+2 is computed and returns value of -9.

Q14: What is associativety of operators? Associativily of Operators:

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

Associativity refers to the order in which operators having the same precedence are evaluated. The operators having the same precedence order are evaluated either from right to left or from left to right. The following table gives the rules for precedence and associativity for some operators in C. Operators () ++(postfix) --(postfix) +(unary) –(unary) ++(prefix) --(prefix) * / % + = += -= *= /= Associativity Left to right Left to right Left to right Left to right Right to left

For example, in the following expression, the + & - operators have the same precedence. Thus the associative rule is used to determine how the expression is evaluated. 1+2+3-4+5 the associativity is from left to right and the expression is evaluated from left to right as: a. 3 + 3 -4 +5 b. 6 – 4 – 5 c. 2 + 5 d. 7

**Q15: Write down the equivalent expressions to indicate the precedence order and associativity of operators?
**

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

No 1 2 3 4 Expression 7+c*d/e b*b – 4*a*c a/b/c 2+a/b*5

Operators

Equivalent Expression (7 + ((c*d)/3)) ((b*b)-((4*a)*c)) ((a/b)/c) (2+((a/b)*5))

**Q16: Write down the equivalent statements to indicate the precedence order and associativity of operators?
**

The assignment operator '=' associated from right to left. Therefore, the value of the expression in the right most expression is first evaluated. No Expression Equivalent Expression 1 a=b=c=d; a=(b=(c=15)); 2 i += j+k; i = i + (j+k); 3 J *= k = m + 5 J = (j *(5 = (m + 5))); 4 J /= k+3; J=j/(k+3);

**Q17: Evaluate the following expression?
**

int a = 1, b = 2, c = 3; A += b += c += 7; After considering the precedence and associativity of operators in the above expression, the equivalent expression is as shown below: A = (a * (b = b +(c = c + 7))); Therefore, the values in the variables after execution of the expression will be: c = 10, b = 12, a = 13

**Q18: What is meant by type casting? Type Casting:
**

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

Conversion of the data type of a numeric value into another data type during arithmetic operations is called casting or type casting. Casting is of two types. These are: Implicit Casting Explicit Casting

Implicit Casting:

An arithmetic operation involving two numeric values can only be performed if the values are of the same type. If these values are of different data types, then the value with a lower order data type is converted into the data type of the other value. The order of data types is: Higher data type Long double double float long int lower data type char

For example, consider the following expression if 'a' is double and 'b' and 'c' are long: a + b –c Considering the precedence order and associativity of the operators, the above expression will be evaluated as: ((a + b) – c) • In the expression, a is double and b is long. So, b will be converted into double and the result will be double. • Next the value of c will be subtracted from the value of (a+b) which is double. The c is long, so the value c will be converted into double and then subtracted. The final answer will be double.

Explicit Casting

In explicit casting, the cast operator is used to convert the numeric data type into another numeric datatype. The syntax of the cast operator is: (type) expression; expression specifies the constant, variable or an expression whose data type is to be converted. type converted. specifies the data type to which the value of the expression is to be

For example, to convert a floating point value 3.14159 into an integer value, the statement is written as: (int) 3.14159; After executing the statement, the floating point value is converted into integer and its decimal portion is truncated. The result will be 3. the cast operator takes the value of its operand and converts it to a value of the type specified in the parentheses. It does not change the value. In the following program, the explicit type casting has been used. After casting, the data type of variable a will be converted into integer (786). Since variable b is of float type, the value of a will again be converted into float type (786.000000) and then assigned to the variable b.

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Program:

#include<stdio.h> #include<conio.h> main() { float a = 786.87, b; b = (int) a; printf ("value of b = %f", b); getch(); } Output Value of b=786.000000

Operators

**Q19: How does the cast take place in the assignment statement: Casting in Assignment Statement
**

When the value of one numeric data type is assigned to a variable of another numeric data type in the assignment statement, the type of the value being assigned is implicitly changed. It is changed to the data type of the variable to which it is assigned. For example int number = 0; float decimal = 316.87f; number = decimal; the data type of the variable number is int. when the float type value decimal is assigned to the integer type variable, it is converted into integer type by truncating its decimal portion and then the integer value is assigned to the variable on the left of the assignment operator. Thus the value in the variable number will be 316.

**Q20: Distinguish between Overflow and Underflow? Overflow and Underflow:
**

Each type of numeric variable has a minimum and maximum value. Any value between these limits can be assigned to it. When a value that exceeds the limits is assigned to a variable, an error occurs. When the value assigned is more than the maximum allowable limit, an overflow error occurs. For example, the maximum exponent value of a floating point value is 37. When the following code is executed, the value assigned to the variable z is 1.0 x 1060. The value exceeds the maximum limits and overflow occurs. foat x, y, z; ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

x = 1.0e30; y = 1.0e30; z = x*y; similarly, when the value is les than the minimum allowable limit, the underflow occurs. For example, the minimum exponent value of a floating point value is -38. When the following code is executed, the value assigned to the variable z is 1.0 x 1060 . This value is less than the minimum limit and underflow occurs. float x, y, z; x = 1.0e-30; y = 1.0e-30; z = x*y; This action taken by the computer in case of overflow and underflow depends upon the compiler and the computer system. In some computer systems, the maximum value is assigned to the variable in case of an overflow. Similarly, a zero value is assigned in case of an underflow.

**Q21: Describe Integer Overflow and Underflow? Integer Overflow and Underflow
**

In case of overflow and underflow in integer variables, the assigned value "wraps around" the maximum/minimum limits. The wrap around means that the value that comes after the maximum is the minimum value.

For example, the maximum value that an int type variable can have is 32767. when 1 is added to a variable that already contains a value of 32767, the result is -32678. that is, the value wraps around and starts from the minimum value, i.e. -32768. Similarly, the value below the minimum limit is the maximum value. The minimum value that an int type variable can have is -32768. When 1 is subtracted from the variable that already contains a value of -32768, the result is 32767. That is, the value wraps around and starts from the maximum value. i.e. 32767. ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

#include<stdio.h> #include<conio.h> #include<limits.h> main() { int max, min; int overmax, undermin; max = INT_MAX; min = INT_MIN; overmax = max + 10; undermin = min – 10;

Operators

printf ("Maximum limit for int variable is = %i\n", max); printf ("Minimum limt for int variable is = %i\n\n", min); printf ("Overflow for int variable is = %i\n", overmax); printf ("Underflow for int variable is = %i\n", undermin); getch(); } Output Maximum limit for int variable is = 32767 Minimum limit for int variable is = -32768 Overflow for int variable is = -32768 Underflow for int variable is = 32767

**Q22: Describe the "sizeof" Operator? The "sizeof" operator:
**

The size of operator is used to find the number of bytes occupied by a data item. Its syntax is: ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com

Chapter 3

Operators

sizeof (operand) the operand may be a constant or a variable. For example, to find the number of bytes occupied by an integer constant I, the expression is written as: sizeof (i) The expression will return a value of 2. Write a program to find the number of bytes occupied by various data types using the sizeof operator #include<conio.h> #include<stdio.h> main() { clrscr(); printf ("Sizeof char type is %d byte\n", sizeof (char)); printf ("Sizeof short type is %d bytes\n", sizeof (short)); printf ("Sizeof int type is %d bytes\n", sizeof (int)); printf ("Sizeof float type is %d bytes\n", sizeof (float)); printf ("Sizeof long type is %d bytes\n", sizeof (long)); printf ("Sizeof double type is %d bytes\n", sizeof (double)); getch(); } Output: Sizeof char type is 1 byte Sizeof short type is 2 bytes Sizeof int type is 2 bytes Sizeof float type is 4 bytes Sizeof long type is 4 bytes Sizeof double type is 8 bytes

ملک طیب عارفemail: tayyab8632@yahoo.com, malik8632@yahoo.com