- 2069 Chaitra · 3 marks
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
++xmakes x = 4 first (pre-increment), then4 * (5-3) / 2= 4 * 2 / 2 = 4 (left to right)z++*yuses 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)
Related questions
- Output of printf statements · Computer Programming, chapter 4
- Correct program: character arrays · Computer Programming, chapter 7
- Correct program: do-while and pow · Computer Programming, chapter 3
- Correct program: Celsius to Fahrenheit · Computer Programming, chapter 3
- Correct program: case array and average · Computer Programming, chapter 3
- Need of stdio.h header file · Computer Programming, chapter 3
- Pyramid pattern output program · Object Oriented Programming, chapter 8
- Cube of int, float, double by overloading · Object Oriented Programming, chapter 3
- Return current path in high-speed circuits · Instrumentation II, chapter 6
- Regular expression and FSA: at least three b's · Discrete Structure, chapter 2