Solved Examples · Example 12
Q.Write a C program to illustrate the compact representation of arithmetic operations.
#include<stdio.h>
void main()
{
int x=2, y=4, z=1;
x+=y;
y-=x;
z*=x;
printf("%d\n",x);
printf("%d\n",y);
printf("%d\n",z);
}
#include<stdio.h>
void main()
{
int x=2, y=4, z=1;
x+=y;
y-=x;
z*=x;
printf("%d\n",x);
printf("%d\n",y);
printf("%d\n",z);
}
Karnataka PUCTextbookLong· 3mImportance★★★★★est
38% · 59/154 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
Compound assignment operators (+=, -=, *=) apply the operation to the variable's current value; evaluating them in order gives x=6, y=-2, z=6.
A shorthand (compound) assignment operator such as x += y is equivalent to x = x + y, and similarly y -= x means y = y - x and z *= x means z = z * x. Each uses the value stored at the instant it runs, so the order matters. Tracing with x = 2, y = 4, z = 1:
x += y;: x = 2 + 4 = 6.y -= x;: y = 4 - 6 = -2 (x is now 6).z *= x;: z = 1 x 6 = 6.
The three printf statements then display x, y and z on separate lines. A complete standard version is:
#include <stdio.h>
int main(void)
{ …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.