Solved Examples · Example 15
Q.Write a C program to illustrate the use of decrement operator.
#include<stdio.h>
void main()
{
int a=7, b=5;
printf("a=%d\h",--a);
printf("b=%d\h",b--);
}
#include<stdio.h>
void main()
{
int a=7, b=5;
printf("a=%d\h",--a);
printf("b=%d\h",b--);
}
Karnataka PUCTextbookLong· 3mImportance★★★★★est
48% · 74/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]
Pre-decrement (--a) yields the value after subtracting one, so it prints 6; post-decrement (b--) yields the value before subtracting, so it prints 5.
The decrement operator -- reduces an integer by one and, like ++, behaves differently depending on its position:
- In pre-decrement
--a, a is decreased first and the new value is used. With a = 7,printf("a=%d", --a)prints 6. - In post-decrement
b--, the current value of b is used first and only then is b decreased. With b = 5,printf("b=%d", b--)prints 5, after which b becomes 4.
A complete, corrected version is:
#include <stdio.h>
int main(void)
{
int a = 7, b = 5;
printf("a=%d\n", --a);
printf("b=%d\n", b--);
return 0;
}
Output:
a=6
b=5 …
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.