Operators Revisited
1. What would be the value of following expressions.
1. 6 * 9 + 2
Ans: Using precedence table, we know order of evaluation would be (6 * 9) + 2 which evaluates to 56.
2. 4 + sizeof(int) * 5
Ans: Assuming sizeof int is 4, this is 4 + 4 * 5. Again using precedence table we determine order of evaluation as 4 + (4 * 5) which evaluates to 24.
3. val = a+b>a+c: sizeof(long double)/4+5? sizeof(long double)*4+5 //Assume a=3, b=5, c=1
Ans: Syntax error. Ternary operator is ? : and not : ?
2. What is the value of following expression.
1. 4*3/5+9
Ans: From precedence table we know that * and / have same precedence which is higher than +. So expression boils down to (4*3/5)+9. * and / have left to right associativity, hence right interpretation is
((4*3)/5)+9
= (12/5) + 9
= 2 + 9
= 11
2. sizeof(int)+sizeof(float)*5.0/2
Ans: Assuming int and float are 4 bytes each, we get
4 + 4 * 5.0 /2
Using precedence and associativity, this expression is interpreted as
4 + ((4 * 5.0)/2)
= 4 + (20.0/2)
= 4 + 10.0
= 14.0
3. (float)100/12/4
Ans: From operator precedence we see that typecast is higher precendence than division. Also division operator has left to right associativity. Hence right interpretation for this expression is
(((float)100)/12)/4
= (100.0/12)/4
= 8.333333 / 4
= 2.083333
3. What are the values of a, b, and c after execution of following expressions, assuming they are int with a = 5, b = 7, c = 11
1. a -= b /= c
Ans: -= and /= have same precedence and right to left associativity. Hence right interpretation of this expression is
a -= ( b /= c ).
b /= c evaluates to
b = b/c
b = 7/11
b = 0
a -= 0 evaluates to
a = a – 0
a = 5.
Hence values of a, b, and c are 5, 0, 11 respectively.
2. a – b/= c
Ans: Note that first operator is – and not -=. – has higher priority – hence it will be interpreted as (a-b) /= c which is same as (a-b) = (a-b)/c. As you can see a-b is not an lvalue, it cannot be assigned a value. This expression will result in a syntax error.
3. a –= (b+c)/a
Ans: / has higher priority than -= and hence this expression will be interpreted as
a -= ((b+c)/a)
a = a – ((b+c)/a)
a = 5 – ((7+11)/5)
a = 5 – (18/5)
a = 5 – 3
a = 2
Hence, values of a, b, c will be 2, 7, 11 respectively.
4. Write a program that reads in scores of five subjects for a student and prints their total and percentage score.
#include <stdio.h> int main(int argc, char * argv[]) { int subject1=0, subject2=0, subject3=0, subject4=0, subject5=0, total=0; float percentage=0; printf("Enter score of first subject [0-100]: "); scanf("%d", &subject1); printf("Enter score of second subject [0-100]: "); scanf("%d", &subject2); printf("Enter score of third subject [0-100]: "); scanf("%d", &subject3); printf("Enter score of fourth subject [0-100]: "); scanf("%d", &subject4); printf("Enter score of fifth subject [0-100]: "); scanf("%d", &subject5); if (subject1 > 100 || subject1 < 0 || subject2 > 100 || subject2 < 0 || subject3 > 100 || subject3 < 0 || subject4 > 100 || subject4 < 0 || subject5 > 100 || subject5 < 0 ) { printf("Invalid input\n"); } else { total = subject1 + subject2 + subject3+ subject4 + subject5; percentage = (float)total/5; /* Note that %% prints % sign. "Format specifiers" are interpreted by printf * and not by C compiler. \ is escape character for entire "format string" * which is processed by C compiler. Hence \ does not escape %. * % is printf escape character */ printf("Total is %d\nPercentage is %.2f%% \n", total, percentage); } return 0; }
5. What are the values of a, b, and c after execution of following expressions, assuming they are int with a = 2, b = 4, c = 8
1. a -= b /= -c
Ans: -= and /= have same precedence and right to left associativity while unary – has higher precedence. Hence this statement will be intrepreted as
a -= (b /= (-c))
or a -= (b /= (-8))
or a -= (b = 4 / (-8))
or a -= (b = 0)
or a -= 0
or a = 2 – 0
a = 2
Hence, values of a, b, and c after this statement will be 2, 0, and 8 respectively.
2. a –= +(b+c)/-a
Ans: Unary + and – have highest precedence. This statement will be interpreted as
a -= ((+(b+c))/(-a))
or a -= ((+(4+8))/(-2))
or a -= (12/-2)
or a -= -6
or a = 2 – -6
or a = 8
Hence, values of a, b, and c after this statement will be 8, 4, and 8 respectively.
6. What are the values of a, b, and c after execution of following code snippets, assuming they are int with a = 2, b = 4, c = 8
1. b += (
2. a –= +(b+c)/-a
Misprint. First is incomplete while second is reprint of Q5.