Skip to main content

Output of arithmetic expression program

Short answer

The output is a=-28b=-31 on one line. a = 3*2 + 5/5 - 7*5 = -28. For b, ++x makes x=4, then 4*(5-3)/2 =4, z++*y uses z=7, so 7*5=35, b=4-35=-31.

Give the output of the following program and justify your answer with reason.

#include<stdio.h>
int main()
{
  int x=3,y=5,z=7;
  int a,b;
  a=x*2+y/5-z*y;
  b=++x*(y-3)/2-z++*y;
  printf("a=%d",a);
  printf("b=%d",b);
  return 0;
}

Output

a=-28b=-31

(The two printf calls have no newline, so both values appear on one line.)

Reason

Initially x = 3, y = 5, z = 7.

For a: a = x*2 + y/5 - z*y

  • x*2 = 6, y/5 = 1, z*y = 35 (multiplication and division are done before + and -)
  • a = 6 + 1 - 35 = -28

For b: b = ++x*(y-3)/2 - z++*y

  • ++x makes x = 4 first (pre-increment), then 4 * (5-3) / 2 = 4 * 2 / 2 = 4 (left to right)
  • z++*y uses the old z = 7: 7 * 5 = 35, and then z becomes 8
  • b = 4 - 35 = -31

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 3 questions →