Evaluate the following expression in C Programming

1 year ago
C Programming

Evaluate the following expression Assume,

int i = 10, b=20,c=5,e=2;     float q =2.5, d=4.5;

  1. a/b + (a/(2*b))
  2. f = ++a + b-- / q find value of f, a, and b
  3. G = b%a++; find value of G and a
  4. H = b%++a; find value of H and a
  5. a+2 < b || !c && a==d || a-2 <=e

i)     a/b + (a/(2*b))

10/20 + ( 10/(2*20)) // substituite values of variables

10/20 + (10/40)           // inner most parentheses is having highest precedence

10/20 + 0                    //parentheses is having highest precedence so evaluate it

                                   //first, 10/40(int/int) so result is 0

0 + 0                            // division operators has higher precdence than +,

                                    //10/20(int/int) so result is 0

0          // overall result of expression

 

ii)      f = ++a + b-- / q    find value of f, a, and b

f = ++10 + 20-- / 2.5               // substiuite values

f = ++10 + 20 / 2.5                  // b =19, since postdecrement(--) operator has

                                                 //higher precedence evaluate it first according to steps

f = 11 + 20 / 2.5                       // a = 11, since preincrement(++) operator has

                                                 //higher precedence evaluate it first according to steps

f = 11 + 8.0                               // 20/2.5(int/float) so result is floating point

                                                  //value(implicit type conversion)

f = 19.0                                      // 11+8.0(int + float) so final result is also float

Therefore, f=19.0, a=11, b=19

 

iii)    G = b%a++; find value of G and a

G = 20%10++;            // substituite values

G = 20%10                  // a=11, postincrement(++) has higher precedence so

                                    // evaluate it first according to steps

G = 0                           // % operator results remainder hence answer is 0

Therefore,      G =0, a=11

 

iv) H = b%++a; find value of H and a

H = 20%++10;            // substituite values

H = 20%11                  // a=11, preincrement(++) has higher precedence so

                                    // evaluate it first according to steps

H = 9                           // % operator results remainder hence answer is 9

Therefore, H = 9, a = 11

 

v) a+2 < b || !c && a==d || a-2 <=e

10 + 2 < 20 || !5 && 10 == 4.5 || 10-2 <= 2               //substituite values

/* Since unary ! operator is having highest precedence so !5 => !true => false(0)

10 + 2 < 20 || 0 && 10 == 4.5 || 10-2 <= 2

/* + and – operator have higher precedence and both have same precedence, so use associativity rule i.e. left to right, hence + is evaluted first */

12 < 20 || 0 && 10==4.5 || 10-2 <= 2

/* - has higher precedence so evaluate it first,10-2 is 8 */

12<20 || 0 && 10 ==4.5 || 8 <=2

/* < and <= have higher precedence and both have same precedence, so use associativity rule i.e. left to right, hence evaluate < first, 12<20 is true so result is 1 */

1|| 0 && 10==4.5 || 8<=2

/* <= has higher precedence so evaluate it first, 8<=2 is false so result is 0 */

1 || 0 && 10==4.5 || 0

/*== has higher precedence so evaluate it first,10==4.5 is false, so result is 0*/

1 || 0 && 0 || 0

/* && has higher precedence so evaluate it first, 0&&0 is 0 */

1 || 0 || 0

// use associativity rule of || operator i.e. left to right and evaluate 1||0 is 1 */

1||0

1          // result of complete expression

Sanisha Maharjan
Jan 21, 2022
More related questions

Questions Bank

View all Questions

Top